{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.668028Z",
     "end_time": "2023-04-03T16:23:41.700858Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.671401Z",
     "end_time": "2023-04-03T16:23:41.701233Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4]\n",
    "# 将list转换为numpy数组\n",
    "nd1 = np.array(l)\n",
    "display(nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.674849Z",
     "end_time": "2023-04-03T16:23:41.701466Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0, 0],\n       [0, 0, 0, 0],\n       [0, 0, 0, 0]], dtype=int16)"
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape=(3, 4), dtype=np.int16)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.677915Z",
     "end_time": "2023-04-03T16:23:41.701584Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.]], dtype=float32)"
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape=(3, 5), dtype=np.float32)\n",
    "nd3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.682610Z",
     "end_time": "2023-04-03T16:23:41.701694Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n\n       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n\n       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])"
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 = np.full(shape=(3, 4, 5), fill_value=3.1415926)\n",
    "nd4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.685811Z",
     "end_time": "2023-04-03T16:23:42.673953Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 8, 33, 97, 47, 40, 54, 28,  3, 92, 82,  6, 75, 63, 48, 82, 50, 27,\n       38, 84, 67])"
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0, 100, size=20)\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.687921Z",
     "end_time": "2023-04-03T16:23:42.674107Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "0.4967536589309791"
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 = np.random.rand()  # 0,1 之间的随机数\n",
    "nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.692535Z",
     "end_time": "2023-04-03T16:23:42.674213Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-0.23556647,  1.04492564, -1.81113861,  0.15267177,  0.841628  ],\n       [ 0.29739208,  0.35951627, -0.98963146,  0.70156341, -0.16253439],\n       [-0.86704936,  2.10228663, -1.63717145,  0.63289251,  0.77410108]])"
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd7 = np.random.randn(3, 5)  # 正态分布，平均是0，标准差是1\n",
    "nd7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.695604Z",
     "end_time": "2023-04-03T16:23:42.818811Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[166.08866786, 175.14703499, 177.18721237, 174.83940911,\n        167.38766137],\n       [185.11938291, 195.96148435, 169.62973675, 167.94139262,\n        155.37857216],\n       [172.37588706, 176.69092191, 167.17367297, 154.69439123,\n        175.86494159]])"
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd8 = np.random.normal(loc=175, scale=10, size=(3, 5))  # 正态分布，平均是loc，标准差是scale\n",
    "nd8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.698708Z",
     "end_time": "2023-04-03T16:23:42.819030Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n       35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,\n       69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99])"
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd9 = np.arange(1, 100, step=2)  #等差数列 左闭右开\n",
    "nd9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.702695Z",
     "end_time": "2023-04-03T16:23:42.819213Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([  0.        ,   2.02020202,   4.04040404,   6.06060606,\n         8.08080808,  10.1010101 ,  12.12121212,  14.14141414,\n        16.16161616,  18.18181818,  20.2020202 ,  22.22222222,\n        24.24242424,  26.26262626,  28.28282828,  30.3030303 ,\n        32.32323232,  34.34343434,  36.36363636,  38.38383838,\n        40.4040404 ,  42.42424242,  44.44444444,  46.46464646,\n        48.48484848,  50.50505051,  52.52525253,  54.54545455,\n        56.56565657,  58.58585859,  60.60606061,  62.62626263,\n        64.64646465,  66.66666667,  68.68686869,  70.70707071,\n        72.72727273,  74.74747475,  76.76767677,  78.78787879,\n        80.80808081,  82.82828283,  84.84848485,  86.86868687,\n        88.88888889,  90.90909091,  92.92929293,  94.94949495,\n        96.96969697,  98.98989899, 101.01010101, 103.03030303,\n       105.05050505, 107.07070707, 109.09090909, 111.11111111,\n       113.13131313, 115.15151515, 117.17171717, 119.19191919,\n       121.21212121, 123.23232323, 125.25252525, 127.27272727,\n       129.29292929, 131.31313131, 133.33333333, 135.35353535,\n       137.37373737, 139.39393939, 141.41414141, 143.43434343,\n       145.45454545, 147.47474747, 149.49494949, 151.51515152,\n       153.53535354, 155.55555556, 157.57575758, 159.5959596 ,\n       161.61616162, 163.63636364, 165.65656566, 167.67676768,\n       169.6969697 , 171.71717172, 173.73737374, 175.75757576,\n       177.77777778, 179.7979798 , 181.81818182, 183.83838384,\n       185.85858586, 187.87878788, 189.8989899 , 191.91919192,\n       193.93939394, 195.95959596, 197.97979798, 200.        ])"
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd10 = np.linspace(0, 200, num=100)  #左闭右闭 生成num个\n",
    "nd10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.706344Z",
     "end_time": "2023-04-03T16:23:42.819284Z"
    }
   },
   "outputs": [],
   "source": [
    "nd = np.random.randn(5, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.708614Z",
     "end_time": "2023-04-03T16:23:42.819402Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(5, 3)"
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.shape  #数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.711663Z",
     "end_time": "2023-04-03T16:23:42.819507Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('float64')"
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype  #数组的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.714592Z",
     "end_time": "2023-04-03T16:23:42.819609Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "15"
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.size  #数组的尺寸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.718009Z",
     "end_time": "2023-04-03T16:23:42.819711Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.ndim  #数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.720340Z",
     "end_time": "2023-04-03T16:23:42.819813Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "8"
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.itemsize  # 单挑数据长度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.727382Z",
     "end_time": "2023-04-03T16:23:42.868383Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[17, 52, 10, 84, 54],\n       [36, 59, 69, 65,  7],\n       [83, 25, 98, 42, 45]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[ 1.81157409,  0.56264801,  1.66864494, -1.10808849,  1.06644268],\n       [ 0.09963842, -1.22935374, -0.51473228, -0.95705208, -0.65340856],\n       [ 0.32699547, -0.13994468,  0.22604112, -0.02453979,  0.44093029]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 100, size=(3, 5))\n",
    "nd2 = np.random.randn(3, 5)\n",
    "display(nd1, nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.731299Z",
     "end_time": "2023-04-03T16:23:42.868477Z"
    }
   },
   "outputs": [],
   "source": [
    "np.save('./data', nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.734094Z",
     "end_time": "2023-04-03T16:23:42.868591Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<function IPython.core.display_functions.display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, raw=False, clear=False, **kwargs)>"
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.load('./data.npy')\n",
    "display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.782850Z",
     "end_time": "2023-04-03T16:23:42.979474Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([7, 4, 2, 7, 4])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([6, 3, 4, 0, 8])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 10, size=5)\n",
    "nd2 = np.random.randint(0, 10, size=5)\n",
    "display(nd1, nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.787113Z",
     "end_time": "2023-04-03T16:23:42.979650Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  1, -2,  7, -4])"
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 - nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.790307Z",
     "end_time": "2023-04-03T16:23:42.979758Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([42, 12,  8,  0, 32])"
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.792871Z",
     "end_time": "2023-04-03T16:23:42.979926Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/g_/dxml3hms52dfwqrcbt4fdmtw0000gn/T/ipykernel_44677/2467826621.py:1: RuntimeWarning: divide by zero encountered in divide\n",
      "  nd1 / nd2\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([1.16666667, 1.33333333, 0.5       ,        inf, 0.5       ])"
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 / nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.796582Z",
     "end_time": "2023-04-03T16:23:42.980030Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([117649,     64,     16,      1,  65536])"
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 ** nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.799378Z",
     "end_time": "2023-04-03T16:23:42.980133Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "4.605170185988092"
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(100)  #自然底数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.801964Z",
     "end_time": "2023-04-03T16:23:42.980233Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "8"
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(2, 3)  #2的3次幂"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.808350Z",
     "end_time": "2023-04-03T16:23:43.004202Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ True,  True, False,  True, False])"
     },
     "execution_count": 234,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 > nd2  # 对应位置进行比较"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组与标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.835926Z",
     "end_time": "2023-04-03T16:23:43.054410Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([17, 14, 12, 17, 14])"
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 + 10  #所有位置都+10，这种操作叫广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.839190Z",
     "end_time": "2023-04-03T16:23:43.054597Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([14,  8,  4, 14,  8])"
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.841547Z",
     "end_time": "2023-04-03T16:23:43.054704Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.14285714, 0.25      , 0.5       , 0.14285714, 0.25      ])"
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### -=\\+=\\*= 直接改变数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.866206Z",
     "end_time": "2023-04-03T16:23:43.084508Z"
    }
   },
   "outputs": [],
   "source": [
    "nd1 += 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.869346Z",
     "end_time": "2023-04-03T16:23:43.084768Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([107, 104, 102, 107, 104])"
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复制和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.920103Z",
     "end_time": "2023-04-03T16:23:43.122156Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=5)\n",
    "b = a  #赋值操作，只是指针指向同一个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.922373Z",
     "end_time": "2023-04-03T16:23:43.122400Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.946872Z",
     "end_time": "2023-04-03T16:23:43.257079Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=10)\n",
    "b = a.view()  #各元素是同一个对象，但数组本身是两个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.949401Z",
     "end_time": "2023-04-03T16:23:43.257360Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.951937Z",
     "end_time": "2023-04-03T16:23:43.257470Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.owndata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.954847Z",
     "end_time": "2023-04-03T16:23:43.257642Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([1024, 2048,    1,    2,    5,    9,    0,    1,    0,    2])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024, 2048,    1,    2,    5,    9,    0,    1,    0,    2])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048\n",
    "display(a, b)  #修改谁都是修改的元素内容本身，都会引起数组变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.958524Z",
     "end_time": "2023-04-03T16:23:43.257697Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=10)\n",
    "b = a.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.960934Z",
     "end_time": "2023-04-03T16:23:43.257861Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([1024,    6,    5,    5,    2,    7,    0,    0,    4,    9])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([   6, 2048,    5,    5,    2,    7,    0,    0,    4,    9])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048\n",
    "display(a, b)  #修改的是数组自己的元素内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:39.964042Z",
     "end_time": "2023-04-03T16:23:43.667332Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n       9.9999998e+07, 9.9999999e+07])"
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1e8)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.045042Z",
     "end_time": "2023-04-03T16:23:43.667433Z"
    }
   },
   "outputs": [],
   "source": [
    "b = a[[1, 3, 5, 7, 99]].copy()  #取出想要的数据后，删除a，防止内存占用\n",
    "del a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引、切片、迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本索引切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.048606Z",
     "end_time": "2023-04-03T16:23:43.667562Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([25,  1,  0, 19,  0, 22, 11, 19, 15, 17])"
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 30, size=10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.051357Z",
     "end_time": "2023-04-03T16:23:43.667676Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "19"
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.053819Z",
     "end_time": "2023-04-03T16:23:43.667781Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1, 19, 22])"
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[[1, 3, 5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.056364Z",
     "end_time": "2023-04-03T16:23:43.667886Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([25,  1,  0])"
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:3]  #左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.058671Z",
     "end_time": "2023-04-03T16:23:43.667995Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([25,  1,  0])"
     },
     "execution_count": 254,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:3]  #：前不写，默认0开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.061080Z",
     "end_time": "2023-04-03T16:23:43.668099Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([19,  0, 22, 11, 19, 15, 17])"
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3:]  #：后不写，默认切到最后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.064010Z",
     "end_time": "2023-04-03T16:23:43.668207Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([25,  0,  0, 11, 15])"
     },
     "execution_count": 256,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2]  # 每2个取1个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.065593Z",
     "end_time": "2023-04-03T16:23:43.668320Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([19, 11, 17])"
     },
     "execution_count": 257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3::3]  #从3开始，每3个取1个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.069021Z",
     "end_time": "2023-04-03T16:23:43.668438Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([17, 15, 19, 11, 22,  0, 19,  0,  1, 25])"
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]  #倒排"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.073627Z",
     "end_time": "2023-04-03T16:23:43.668546Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1, 19, 22])"
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:7:2]  #索引1开始，索引7结束，每2个取1个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.076535Z",
     "end_time": "2023-04-03T16:23:43.668656Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 4, 19,  1, 29,  1, 22,  7, 18, 13,  3],\n       [ 9,  8,  5, 13,  5, 13, 20, 20,  1, 17],\n       [ 6,  2,  3, 15, 21,  9, 13, 16, 29, 22],\n       [12,  0,  2,  0,  4, 16,  1, 28, 10, 15],\n       [19,  3, 17,  8, 29, 27, 24, 25, 25, 28],\n       [16, 25,  4, 27, 26, 15,  7, 27, 10, 22],\n       [ 8,  3,  6, 24, 26,  5, 25, 19, 18, 13],\n       [21, 15, 21, 25,  6, 13, 19, 19,  0, 28],\n       [28,  9, 18, 29, 20, 26, 12,  6, 25, 16],\n       [ 0, 24, 19, 25,  5, 27, 17, 19,  8, 14]])"
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0, 30, size=(10, 10))\n",
    "b  #二维数组和一维一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.079108Z",
     "end_time": "2023-04-03T16:23:43.668759Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 9,  8,  5, 13,  5, 13, 20, 20,  1, 17])"
     },
     "execution_count": 261,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.081846Z",
     "end_time": "2023-04-03T16:23:43.668864Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 2, 16,  1])"
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[3, [2, 5, 6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.084744Z",
     "end_time": "2023-04-03T16:23:43.669011Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 2, 21, 16],\n       [ 0,  4, 28],\n       [ 3, 29, 25],\n       [25, 26, 27],\n       [ 3, 26, 19]])"
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2:7, 1::3]  #行：2-7，列：1开始，每3个取1个"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.087189Z",
     "end_time": "2023-04-03T16:23:43.669119Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "execution_count": 264,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.090103Z",
     "end_time": "2023-04-03T16:23:43.669223Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([3, 4, 5, 6])"
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[3:7]  #切片返回的数据不是深拷贝\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.092472Z",
     "end_time": "2023-04-03T16:23:43.727257Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([   0,    1,    2, 1024,    4,    5,    6,    7,    8,    9,   10,\n         11,   12,   13,   14,   15,   16,   17,   18,   19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024,    4,    5,    6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.096324Z",
     "end_time": "2023-04-03T16:23:43.727551Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([3, 4, 5, 6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "b = a[[3, 4, 5, 6]]  #花式索引：索引时给出索引的数组，是深拷贝方式\n",
    "display(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.098976Z",
     "end_time": "2023-04-03T16:23:43.727719Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024,    4,    5,    6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.102997Z",
     "end_time": "2023-04-03T16:23:43.727870Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[106,  85,  88],\n       [ 33, 141,  53],\n       [115,  18,  50],\n       [107, 118, 121],\n       [ 62,  60,  34],\n       [147,  59, 147],\n       [104,  87,  14],\n       [ 84,  23,  74],\n       [121,  64,  89],\n       [ 45,  40, 141],\n       [ 32, 143,  18],\n       [ 84,   7, 101],\n       [  8, 115,  98],\n       [ 61,   6,  27],\n       [ 75, 104,  48],\n       [ 38, 122,  75],\n       [ 84,  72,  51],\n       [136, 130, 125],\n       [ 76, 102, 150],\n       [109, 133,  37],\n       [ 22,  58,  43],\n       [ 77,  40,  69],\n       [ 68,  94,  55],\n       [119,  33, 102],\n       [  6,  42,  92],\n       [143, 149, 133],\n       [ 75,  15, 127],\n       [ 43,  67,  20],\n       [ 24,  73, 146],\n       [ 25,  66,  10],\n       [ 42,  35,   6],\n       [130,  65,  97],\n       [147,  39,  30],\n       [ 85, 149,  81],\n       [121, 142, 101],\n       [  5,  59, 122],\n       [ 26,  59,  22],\n       [147,  61,  50],\n       [ 71, 140, 106],\n       [ 14,  13,  68],\n       [ 88, 148,  38],\n       [ 10,  57,  84],\n       [116,  45,  41],\n       [115, 122, 110],\n       [ 27, 145,  86],\n       [ 53, 106,  26],\n       [ 41, 150,  91],\n       [ 46,  87,  83],\n       [134,  15, 103],\n       [ 15,   8, 134],\n       [144,  48,  73],\n       [  0,  33,  68],\n       [  8, 130,  74],\n       [111, 146,  92],\n       [ 21,  19, 137],\n       [ 54, 125,   5],\n       [ 35,  44,  21],\n       [ 95, 119,  39],\n       [ 89,  24,  66],\n       [ 70,  12,  43],\n       [102,  12,  35],\n       [ 21,  39,  44],\n       [ 73,  93,  96],\n       [ 69,  97,  20],\n       [123,  43, 141],\n       [ 83,  73, 130],\n       [124,  22,  25],\n       [ 14,  54,   1],\n       [116,  51, 143],\n       [120,  71,  15],\n       [ 82,  90,  65],\n       [116,  16,  63],\n       [145, 150, 127],\n       [ 89,  17,  51],\n       [ 71,   2,  31],\n       [122,  45,  58],\n       [112,  87, 145],\n       [  9,  78,  89],\n       [ 76,  91,  25],\n       [ 97,  97,  38],\n       [  8,  77,  59],\n       [ 99,  43, 137],\n       [102,   2,  73],\n       [  5,  26, 112],\n       [ 39,   4,  64],\n       [ 93,   4,  15],\n       [ 55,   3,  34],\n       [111, 106, 133],\n       [ 58,  74,  91],\n       [  1, 125,  26],\n       [112, 141, 148],\n       [110,  53,   0],\n       [132, 127,  51],\n       [111, 139,  55],\n       [ 73,  72,  19],\n       [ 48, 103, 101],\n       [ 78,  58,  91],\n       [ 50, 111,  55],\n       [ 32, 127,  45],\n       [133,  70,  12]])"
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 151, size=(100, 3))  #100名学生，3门考试的成绩\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.107479Z",
     "end_time": "2023-04-03T16:23:43.727989Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([141, 121, 147, 147, 121, 141, 143, 122, 136, 130, 125, 150, 133,\n       143, 149, 133, 127, 146, 130, 147, 149, 121, 142, 122, 147, 140,\n       148, 122, 145, 150, 134, 134, 144, 130, 146, 137, 125, 123, 141,\n       130, 124, 143, 120, 145, 150, 127, 122, 145, 137, 133, 125, 141,\n       148, 132, 127, 139, 127, 133])"
     },
     "execution_count": 270,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = a >= 120  #根据条件筛选数据\n",
    "a[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.110846Z",
     "end_time": "2023-04-03T16:23:43.728093Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[136, 130, 125],\n       [143, 149, 133],\n       [145, 150, 127]])"
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond2 = cond[:, 0] * cond[:, 1] * cond[:, 2]  #3个都>=120\n",
    "a[cond2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.114377Z",
     "end_time": "2023-04-03T16:23:43.728196Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([], shape=(0, 3), dtype=int64)"
     },
     "execution_count": 272,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = a >= 120\n",
    "cond2 = a <= 30\n",
    "a[cond2[:, 0] * cond2[:, 1] * cond2[:, 2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组变形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.119696Z",
     "end_time": "2023-04-03T16:23:43.728300Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 8, 17, 17, 16, 10],\n       [12, 17,  7,  0,  1],\n       [18,  4, 10,  1, 12]])"
     },
     "execution_count": 273,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 20, size=(3, 5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.122707Z",
     "end_time": "2023-04-03T16:23:43.728409Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 8, 17, 17, 16, 10],\n       [12, 17,  7,  0,  1],\n       [18,  4, 10,  1, 12]])"
     },
     "execution_count": 274,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5, 3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.125977Z",
     "end_time": "2023-04-03T16:23:43.815354Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[ 8]],\n\n       [[17]],\n\n       [[17]],\n\n       [[16]],\n\n       [[10]],\n\n       [[12]],\n\n       [[17]],\n\n       [[ 7]],\n\n       [[ 0]],\n\n       [[ 1]],\n\n       [[18]],\n\n       [[ 4]],\n\n       [[10]],\n\n       [[ 1]],\n\n       [[12]]])"
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15, 1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.129601Z",
     "end_time": "2023-04-03T16:23:43.815516Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 8, 17, 17],\n       [16, 10, 12],\n       [17,  7,  0],\n       [ 1, 18,  4],\n       [10,  1, 12]])"
     },
     "execution_count": 276,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1, 3)  #-1表示数据，3自动计算-1=5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.132946Z",
     "end_time": "2023-04-03T16:23:43.815632Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 8, 12, 18],\n       [17, 17,  4],\n       [17,  7, 10],\n       [16,  0,  1],\n       [10,  1, 12]])"
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T  #行变列，列变行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.135522Z",
     "end_time": "2023-04-03T16:23:43.815738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 8, 12, 18],\n       [17, 17,  4],\n       [17,  7, 10],\n       [16,  0,  1],\n       [10,  1, 12]])"
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a, (1, 0))  #与a.T相同，行0，列1，调整顺序(0,1)->(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.138288Z",
     "end_time": "2023-04-03T16:23:43.815841Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[8, 1, 7, 0, 8, 3, 1],\n        [0, 4, 7, 0, 8, 6, 5],\n        [4, 5, 8, 8, 6, 8, 6],\n        [5, 9, 2, 7, 9, 9, 6],\n        [2, 2, 7, 4, 0, 1, 5]],\n\n       [[3, 4, 7, 8, 8, 3, 5],\n        [3, 2, 1, 7, 3, 8, 7],\n        [9, 9, 2, 3, 0, 7, 6],\n        [7, 7, 8, 9, 3, 2, 3],\n        [5, 0, 8, 7, 0, 2, 1]],\n\n       [[5, 1, 5, 1, 3, 6, 2],\n        [3, 0, 2, 3, 8, 8, 4],\n        [5, 9, 5, 7, 7, 2, 2],\n        [4, 7, 1, 5, 1, 8, 2],\n        [4, 2, 4, 5, 6, 1, 6]]])"
     },
     "execution_count": 279,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0, 10, size=(3, 5, 7))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.142332Z",
     "end_time": "2023-04-03T16:23:43.815950Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[8, 3, 5],\n        [0, 3, 3],\n        [4, 9, 5],\n        [5, 7, 4],\n        [2, 5, 4]],\n\n       [[1, 4, 1],\n        [4, 2, 0],\n        [5, 9, 9],\n        [9, 7, 7],\n        [2, 0, 2]],\n\n       [[7, 7, 5],\n        [7, 1, 2],\n        [8, 2, 5],\n        [2, 8, 1],\n        [7, 8, 4]],\n\n       [[0, 8, 1],\n        [0, 7, 3],\n        [8, 3, 7],\n        [7, 9, 5],\n        [4, 7, 5]],\n\n       [[8, 8, 3],\n        [8, 3, 8],\n        [6, 0, 7],\n        [9, 3, 1],\n        [0, 0, 6]],\n\n       [[3, 3, 6],\n        [6, 8, 8],\n        [8, 7, 2],\n        [9, 2, 8],\n        [1, 2, 1]],\n\n       [[1, 5, 2],\n        [5, 7, 4],\n        [6, 6, 2],\n        [6, 3, 2],\n        [5, 1, 6]]])"
     },
     "execution_count": 280,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b, (2, 1, 0))  #(0,1,2)->(2,1,0)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.144927Z",
     "end_time": "2023-04-03T16:23:43.816049Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(7, 5, 3)"
     },
     "execution_count": 281,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据堆叠合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.154152Z",
     "end_time": "2023-04-03T16:23:43.894317Z"
    }
   },
   "outputs": [],
   "source": [
    "nd1 = np.random.randint(0, 10, size=(3, 5))\n",
    "nd2 = np.random.randint(0, 10, size=(3, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.157817Z",
     "end_time": "2023-04-03T16:23:43.894604Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 4, 4, 5, 7],\n       [0, 9, 1, 1, 0],\n       [5, 3, 9, 9, 9],\n       [8, 5, 0, 6, 8],\n       [3, 2, 9, 5, 6],\n       [1, 2, 7, 8, 0]])"
     },
     "execution_count": 283,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1, nd2])  #默认合并后行增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 284,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.160517Z",
     "end_time": "2023-04-03T16:23:43.894710Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 4, 4, 5, 7, 8, 5, 0, 6, 8],\n       [0, 9, 1, 1, 0, 3, 2, 9, 5, 6],\n       [5, 3, 9, 9, 9, 1, 2, 7, 8, 0]])"
     },
     "execution_count": 284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1, nd2], axis=1)  #axis轴，第几个维度，行0，列1，列增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.163995Z",
     "end_time": "2023-04-03T16:23:43.894828Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 4, 4, 5, 7, 8, 5, 0, 6, 8],\n       [0, 9, 1, 1, 0, 3, 2, 9, 5, 6],\n       [5, 3, 9, 9, 9, 1, 2, 7, 8, 0]])"
     },
     "execution_count": 285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((nd1, nd2))  #堆叠，h水平，列增多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.166027Z",
     "end_time": "2023-04-03T16:23:43.894933Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 4, 4, 5, 7],\n       [0, 9, 1, 1, 0],\n       [5, 3, 9, 9, 9],\n       [8, 5, 0, 6, 8],\n       [3, 2, 9, 5, 6],\n       [1, 2, 7, 8, 0]])"
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((nd1, nd2))  #堆叠，v竖直，行增多"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.186214Z",
     "end_time": "2023-04-03T16:23:43.990429Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 100, size=(15, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.189294Z",
     "end_time": "2023-04-03T16:23:43.990691Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[75, 93, 40, 17, 72,  9, 87,  2, 19, 68],\n        [ 8, 24,  8, 87, 58, 86, 45, 59, 35, 12],\n        [89, 39,  0, 92, 67, 86, 31, 95,  3, 29],\n        [59, 19, 36, 75, 43, 65, 26, 42, 64, 37],\n        [86, 49, 60, 44, 59, 17, 76, 19, 29, 81]]),\n array([[17, 97,  6, 74, 11, 28, 89, 67, 58, 97],\n        [57, 77, 82, 37, 69, 94, 14, 85,  3, 55],\n        [71, 75,  5, 97, 85, 12, 86, 33, 90, 60],\n        [37, 70,  1, 83, 39, 19, 22,  3, 54, 62],\n        [59, 47, 94, 89, 26, 42, 62, 35, 66, 95]]),\n array([[51, 93, 91, 17, 18, 42, 69, 36, 45, 65],\n        [12, 43, 91, 94, 72, 90, 36, 26, 94,  8],\n        [37, 27, 93, 27, 80, 82, 55, 75, 20, 38],\n        [48,  0, 35, 46, 57, 20,  5, 25,  3, 27],\n        [80, 85, 76,  0, 56, 18, 30, 41, 29, 30]])]"
     },
     "execution_count": 288,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=3)  #平局分成多少份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.192478Z",
     "end_time": "2023-04-03T16:23:43.990810Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[75, 93, 40, 17, 72],\n        [ 8, 24,  8, 87, 58],\n        [89, 39,  0, 92, 67],\n        [59, 19, 36, 75, 43],\n        [86, 49, 60, 44, 59],\n        [17, 97,  6, 74, 11],\n        [57, 77, 82, 37, 69],\n        [71, 75,  5, 97, 85],\n        [37, 70,  1, 83, 39],\n        [59, 47, 94, 89, 26],\n        [51, 93, 91, 17, 18],\n        [12, 43, 91, 94, 72],\n        [37, 27, 93, 27, 80],\n        [48,  0, 35, 46, 57],\n        [80, 85, 76,  0, 56]]),\n array([[ 9, 87,  2, 19, 68],\n        [86, 45, 59, 35, 12],\n        [86, 31, 95,  3, 29],\n        [65, 26, 42, 64, 37],\n        [17, 76, 19, 29, 81],\n        [28, 89, 67, 58, 97],\n        [94, 14, 85,  3, 55],\n        [12, 86, 33, 90, 60],\n        [19, 22,  3, 54, 62],\n        [42, 62, 35, 66, 95],\n        [42, 69, 36, 45, 65],\n        [90, 36, 26, 94,  8],\n        [82, 55, 75, 20, 38],\n        [20,  5, 25,  3, 27],\n        [18, 30, 41, 29, 30]])]"
     },
     "execution_count": 289,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=2, axis=1)  #axis = 1 表示列被平均分成2分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 290,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.196385Z",
     "end_time": "2023-04-03T16:23:43.990919Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[75, 93, 40, 17, 72,  9, 87,  2, 19, 68]]),\n array([[ 8, 24,  8, 87, 58, 86, 45, 59, 35, 12],\n        [89, 39,  0, 92, 67, 86, 31, 95,  3, 29],\n        [59, 19, 36, 75, 43, 65, 26, 42, 64, 37],\n        [86, 49, 60, 44, 59, 17, 76, 19, 29, 81]]),\n array([[17, 97,  6, 74, 11, 28, 89, 67, 58, 97],\n        [57, 77, 82, 37, 69, 94, 14, 85,  3, 55],\n        [71, 75,  5, 97, 85, 12, 86, 33, 90, 60],\n        [37, 70,  1, 83, 39, 19, 22,  3, 54, 62]]),\n array([[59, 47, 94, 89, 26, 42, 62, 35, 66, 95],\n        [51, 93, 91, 17, 18, 42, 69, 36, 45, 65],\n        [12, 43, 91, 94, 72, 90, 36, 26, 94,  8],\n        [37, 27, 93, 27, 80, 82, 55, 75, 20, 38],\n        [48,  0, 35, 46, 57, 20,  5, 25,  3, 27],\n        [80, 85, 76,  0, 56, 18, 30, 41, 29, 30]])]"
     },
     "execution_count": 290,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=[1, 5, 9])  #0-1,1-5,5-9 根据索引切分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.198147Z",
     "end_time": "2023-04-03T16:23:43.991031Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[75, 93, 40, 17, 72],\n        [ 8, 24,  8, 87, 58],\n        [89, 39,  0, 92, 67],\n        [59, 19, 36, 75, 43],\n        [86, 49, 60, 44, 59],\n        [17, 97,  6, 74, 11],\n        [57, 77, 82, 37, 69],\n        [71, 75,  5, 97, 85],\n        [37, 70,  1, 83, 39],\n        [59, 47, 94, 89, 26],\n        [51, 93, 91, 17, 18],\n        [12, 43, 91, 94, 72],\n        [37, 27, 93, 27, 80],\n        [48,  0, 35, 46, 57],\n        [80, 85, 76,  0, 56]]),\n array([[ 9, 87,  2, 19, 68],\n        [86, 45, 59, 35, 12],\n        [86, 31, 95,  3, 29],\n        [65, 26, 42, 64, 37],\n        [17, 76, 19, 29, 81],\n        [28, 89, 67, 58, 97],\n        [94, 14, 85,  3, 55],\n        [12, 86, 33, 90, 60],\n        [19, 22,  3, 54, 62],\n        [42, 62, 35, 66, 95],\n        [42, 69, 36, 45, 65],\n        [90, 36, 26, 94,  8],\n        [82, 55, 75, 20, 38],\n        [20,  5, 25,  3, 27],\n        [18, 30, 41, 29, 30]])]"
     },
     "execution_count": 291,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a, indices_or_sections=2)  #h水平，列方向平均分成2份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.202739Z",
     "end_time": "2023-04-03T16:23:43.991138Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[75, 93, 40, 17, 72,  9, 87,  2, 19, 68],\n        [ 8, 24,  8, 87, 58, 86, 45, 59, 35, 12],\n        [89, 39,  0, 92, 67, 86, 31, 95,  3, 29]]),\n array([[59, 19, 36, 75, 43, 65, 26, 42, 64, 37],\n        [86, 49, 60, 44, 59, 17, 76, 19, 29, 81],\n        [17, 97,  6, 74, 11, 28, 89, 67, 58, 97],\n        [57, 77, 82, 37, 69, 94, 14, 85,  3, 55]]),\n array([[71, 75,  5, 97, 85, 12, 86, 33, 90, 60],\n        [37, 70,  1, 83, 39, 19, 22,  3, 54, 62],\n        [59, 47, 94, 89, 26, 42, 62, 35, 66, 95],\n        [51, 93, 91, 17, 18, 42, 69, 36, 45, 65]]),\n array([[12, 43, 91, 94, 72, 90, 36, 26, 94,  8],\n        [37, 27, 93, 27, 80, 82, 55, 75, 20, 38],\n        [48,  0, 35, 46, 57, 20,  5, 25,  3, 27],\n        [80, 85, 76,  0, 56, 18, 30, 41, 29, 30]])]"
     },
     "execution_count": 292,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a, indices_or_sections=[3, 7, 11])  #v竖直，行方向平均分成2份"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 广播机制"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "execution_count": 293,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0, 1, 2, 3] * 3)\n",
    "arr1.sort()  #排序，从小到大\n",
    "arr1 = arr1.reshape(4, 3)\n",
    "arr1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.214169Z",
     "end_time": "2023-04-03T16:23:44.116732Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1, 2, 3])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.array([1, 2, 3])\n",
    "display(arr1, arr2)  #形状不对应，但依然可以运算，numpy底层进行了广播"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.217360Z",
     "end_time": "2023-04-03T16:23:44.116953Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 2, 3],\n       [2, 3, 4],\n       [3, 4, 5],\n       [4, 5, 6]])"
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行不够，广播行\n",
    "arr1 + arr2  #arr2和arr1的每一行进行相加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.220783Z",
     "end_time": "2023-04-03T16:23:44.117066Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[1],\n       [2],\n       [3],\n       [4]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([[1], [2], [3], [4]])\n",
    "display(arr1, arr3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.223824Z",
     "end_time": "2023-04-03T16:23:44.117220Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 1, 1],\n       [3, 3, 3],\n       [5, 5, 5],\n       [7, 7, 7]])"
     },
     "execution_count": 297,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列不够，广播列\n",
    "arr1 + arr3  #arr3和arr1的每一列进行相加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.226507Z",
     "end_time": "2023-04-03T16:23:44.117324Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]],\n\n       [[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]],\n\n       [[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]]])"
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0, 1, 2, 3, 4, 5, 6, 7] * 3).reshape(3, 4, 2)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.229938Z",
     "end_time": "2023-04-03T16:23:44.117452Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3],\n       [4, 5],\n       [6, 7]])"
     },
     "execution_count": 299,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([0, 1, 2, 3, 4, 5, 6, 7]).reshape(4, 2)\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.232723Z",
     "end_time": "2023-04-03T16:23:44.117563Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]],\n\n       [[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]],\n\n       [[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]]])"
     },
     "execution_count": 300,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b  #b广播了3份后，与a做求和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.235311Z",
     "end_time": "2023-04-03T16:23:44.117670Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 通用函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 元素级和数字级方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1, -3, -5,  1,  5,  8,  9])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([-1,  3,  3, -1,  9,  5,  0])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.array([-1, -3, -5, 1, 5, 8, 9])\n",
    "c = np.random.randint(-5, 10, size=7)\n",
    "display(a, c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.270875Z",
     "end_time": "2023-04-03T16:23:44.559425Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 3, 5, 1, 5, 8, 9])"
     },
     "execution_count": 302,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(a)  #求绝对值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.272889Z",
     "end_time": "2023-04-03T16:23:44.563675Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  4,  9, 16, 36, 49])"
     },
     "execution_count": 303,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1, 4, 9, 16, 36, 49])\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.274828Z",
     "end_time": "2023-04-03T16:23:44.563812Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 2., 3., 4., 6., 7.])"
     },
     "execution_count": 304,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(b)  #开平方"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.278118Z",
     "end_time": "2023-04-03T16:23:44.563919Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "outputs": [
    {
     "data": {
      "text/plain": "array([   1,   16,   81,  256, 1296, 2401])"
     },
     "execution_count": 305,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(b)  #平方"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.280107Z",
     "end_time": "2023-04-03T16:23:44.564033Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "outputs": [
    {
     "data": {
      "text/plain": "7.38905609893065"
     },
     "execution_count": 306,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(2)  #自然底数e的n次幂"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.283771Z",
     "end_time": "2023-04-03T16:23:44.564141Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "outputs": [
    {
     "data": {
      "text/plain": "2.995732273553991"
     },
     "execution_count": 307,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(20)  #自然底数对数求解"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.286781Z",
     "end_time": "2023-04-03T16:23:44.564256Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 308,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(np.pi / 2)  #正弦值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.289399Z",
     "end_time": "2023-04-03T16:23:44.564364Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 309,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(0)  #余弦值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.292005Z",
     "end_time": "2023-04-03T16:23:44.637767Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9999999999999999"
     },
     "execution_count": 310,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tan(np.pi / 4)  #正切值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.296607Z",
     "end_time": "2023-04-03T16:23:44.638005Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1,  3,  3,  1,  9,  8,  9])"
     },
     "execution_count": 311,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.maximum(a, c)  #从a和c中选取最大的值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.299108Z",
     "end_time": "2023-04-03T16:23:44.638136Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1, -3, -5, -1,  5,  5,  0])"
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.minimum(a, c)  #从a和c中选取最小的值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.301825Z",
     "end_time": "2023-04-03T16:23:44.638261Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 3, 0])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([-1, -3,  4,  8])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.array([1, 3, 0])\n",
    "nd2 = np.array([-1, -3, 4, 8])\n",
    "display(nd1, nd2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.305259Z",
     "end_time": "2023-04-03T16:23:44.638441Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 314,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.any()  #只要有True，0：false，非0：true"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.309285Z",
     "end_time": "2023-04-03T16:23:44.638545Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.all()  #所有都是True"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.311652Z",
     "end_time": "2023-04-03T16:23:44.638644Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "outputs": [
    {
     "data": {
      "text/plain": "60"
     },
     "execution_count": 316,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([1, 2, 3, 4, 6])\n",
    "np.inner(a, b)  #内积 1*1+2*2+3*3+4*4+5*6"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.314780Z",
     "end_time": "2023-04-03T16:23:44.638751Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "outputs": [
    {
     "data": {
      "text/plain": "array([53, 19, 29, 52, 95, 49, 66,  4, 64,  9, 41, 75, 19, 63, 42, 79,  6,\n       83, 74,  6, 23, 50, 43, 84, 32, 67, 40, 82, 90,  3])"
     },
     "execution_count": 317,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 100, size=30)\n",
    "nd1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.317362Z",
     "end_time": "2023-04-03T16:23:44.638858Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "outputs": [
    {
     "data": {
      "text/plain": "array([53, 19, 29, 52, 80, 49, 66, 10, 64, 10, 41, 75, 19, 63, 42, 79, 10,\n       80, 74, 10, 23, 50, 43, 80, 32, 67, 40, 80, 80, 10])"
     },
     "execution_count": 318,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.clip(nd1, 10, 80)  #小于10的变成10，大于80的变成80"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.320278Z",
     "end_time": "2023-04-03T16:23:44.638964Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-0.79206527, -1.0331843 , -0.26679847, -1.57178309,  0.37470647,\n        0.53050572,  0.01356293, -0.11627868, -1.39123016, -1.7982375 ,\n        0.03140136,  1.86464362,  0.42565816,  0.10391457,  0.86060547,\n       -2.18280373, -0.17516879,  1.22799467,  1.31515858, -0.61453379])"
     },
     "execution_count": 319,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.random.randn(20)\n",
    "nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.322425Z",
     "end_time": "2023-04-03T16:23:44.639101Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-0.79, -1.03, -0.27, -1.57,  0.37,  0.53,  0.01, -0.12, -1.39,\n       -1.8 ,  0.03,  1.86,  0.43,  0.1 ,  0.86, -2.18, -0.18,  1.23,\n        1.32, -0.61])"
     },
     "execution_count": 320,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.round(2)  #保留小数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:44.712827Z",
     "end_time": "2023-04-03T16:23:44.712938Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "outputs": [
    {
     "data": {
      "text/plain": "3.0"
     },
     "execution_count": 321,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(2.7)  #向上取整"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.329715Z",
     "end_time": "2023-04-03T16:23:44.713060Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "outputs": [
    {
     "data": {
      "text/plain": "array([3., 3., 3.])"
     },
     "execution_count": 322,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(np.array([2.7, 2.1, 2.05]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.331149Z",
     "end_time": "2023-04-03T16:23:44.713179Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2., 2., 2.])"
     },
     "execution_count": 323,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(np.array([2.9, 2.1, 2.999]))  #向下取整"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.333884Z",
     "end_time": "2023-04-03T16:23:44.713289Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "outputs": [
    {
     "data": {
      "text/plain": "14"
     },
     "execution_count": 324,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, size=(3, 3))\n",
    "np.trace(a)  #计算对角线上的和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.335727Z",
     "end_time": "2023-04-03T16:23:44.713404Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### where函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "nd1 = np.array([1, 3, 5, 7, 9])\n",
    "nd2 = np.array([2, 4, 6, 8, 10])\n",
    "cond = np.array([True, False, False, True, True])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.339561Z",
     "end_time": "2023-04-03T16:23:44.713458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 4, 6, 7, 9])"
     },
     "execution_count": 326,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(cond, nd1, nd2)  #条件如果是True返回nd1的数据，如果是False返回nd2的数据"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.342038Z",
     "end_time": "2023-04-03T16:23:44.713579Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "outputs": [
    {
     "data": {
      "text/plain": "array([99, 93, 19, 37, 63, 72, 90, 34, 86, 27, 19, 45, 73, 61, 65, 64, 68,\n       23, 91, 82, 70, 34, 62, 46, 32, 15, 97, 85,  6, 33,  4, 77, 39, 27,\n       72, 36, 13, 15, 75, 24, 71, 86, 61, 11,  7, 99,  6, 33, 70, 51])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([  99,   93, -100, -100,   63,   72,   90, -100,   86, -100, -100,\n       -100,   73,   61,   65,   64,   68, -100,   91,   82,   70, -100,\n         62, -100, -100, -100,   97,   85, -100, -100, -100,   77, -100,\n       -100,   72, -100, -100, -100,   75, -100,   71,   86,   61, -100,\n       -100,   99, -100, -100,   70,   51])"
     },
     "execution_count": 327,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=50)\n",
    "display(a)\n",
    "np.where(a > 50, a, -100)  #大于50从a中取出，否则返回-100"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.345158Z",
     "end_time": "2023-04-03T16:23:44.713753Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "outputs": [
    {
     "data": {
      "text/plain": "array([53,  3, 62,  8, 19, 43, 34, 28, 78, 20, 65, 49, 22, 63, 76, 54, 51,\n       51, 84, 21, 90, 80, 47,  5,  7, 22, 67, 50, 85, 48, 31, 94, 17, 56,\n       94, 45, 63, 60, 82, 19, 88,  3, 10, 72, 50, 50, 96, 84, 87, 85])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([63,  3, 62,  8, 19, 43, 34, 28, 78, 20, 65, 49, 22, 63, 76, 64, 61,\n       61, 84, 21, 90, 80, 47,  5,  7, 22, 67, 60, 85, 48, 31, 94, 17, 66,\n       94, 45, 63, 60, 82, 19, 88,  3, 10, 72, 60, 60, 96, 84, 87, 85])"
     },
     "execution_count": 328,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分数在50到59的自动加10\n",
    "a = np.random.randint(0, 100, size=50)\n",
    "display(a)\n",
    "cond = (a >= 50) & (a < 60)\n",
    "np.where(cond, a + 10, a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.351933Z",
     "end_time": "2023-04-03T16:23:44.713924Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "outputs": [
    {
     "data": {
      "text/plain": "array([82,  3, 80, 48, 50,  6, 34, 41,  4, 79,  8, 73, 28, 64,  2,  7, 35,\n       75, 60, 60])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([ 2,  3,  4,  6,  7,  8, 28, 34, 35, 41, 48, 50, 60, 60, 64, 73, 75,\n       79, 80, 82])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=20)\n",
    "b = np.sort(a)  #原数组不改变\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.355525Z",
     "end_time": "2023-04-03T16:23:44.775067Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "outputs": [],
   "source": [
    "a.sort()  #在原数组做的排序"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.358257Z",
     "end_time": "2023-04-03T16:23:44.775232Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "execution_count": 331,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = a.argsort()  #返回排序的索引\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.360299Z",
     "end_time": "2023-04-03T16:23:44.775371Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 2,  3,  4,  6,  7,  8, 28, 34, 35, 41, 48, 50, 60, 60, 64, 73, 75,\n       79, 80, 82])"
     },
     "execution_count": 332,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#根据索引取出数据\n",
    "a[index]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.362880Z",
     "end_time": "2023-04-03T16:23:44.775481Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 集合操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 7, 13, 27, 24, 10, 18,  9,  8, 19,  8,  8, 15, 19,  7,  6])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([12, 23, 16, 28, 18, 27, 13, 26, 23,  7, 23, 28, 11, 23, 14])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 30, size=15)\n",
    "b = np.random.randint(0, 30, size=15)\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.366594Z",
     "end_time": "2023-04-03T16:23:44.775655Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 7, 13, 18, 27])"
     },
     "execution_count": 334,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(a, b)  #交集"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.369886Z",
     "end_time": "2023-04-03T16:23:44.775774Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 23, 24, 26, 27,\n       28])"
     },
     "execution_count": 335,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.union1d(a, b)  #并集"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.372592Z",
     "end_time": "2023-04-03T16:23:44.775896Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6,  8,  9, 10, 15, 19, 24])"
     },
     "execution_count": 336,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a, b)  #差集，a中有，b中没有"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.374914Z",
     "end_time": "2023-04-03T16:23:44.776005Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 统计函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[94,  4, 16, 90,  3],\n       [50, 40, 11, 98, 48],\n       [57, 82,  3, 78,  1]])"
     },
     "execution_count": 337,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=(3, 5))\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.392275Z",
     "end_time": "2023-04-03T16:23:45.060901Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 338,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.393756Z",
     "end_time": "2023-04-03T16:23:45.061223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "outputs": [
    {
     "data": {
      "text/plain": "array([94, 82, 16, 98, 48])"
     },
     "execution_count": 339,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis=0)  #方向，axis=0行，axis=1列"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.396592Z",
     "end_time": "2023-04-03T16:23:45.061344Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "outputs": [
    {
     "data": {
      "text/plain": "45.0"
     },
     "execution_count": 340,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean()  #平均值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.399443Z",
     "end_time": "2023-04-03T16:23:45.061458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "outputs": [
    {
     "data": {
      "text/plain": "48.0"
     },
     "execution_count": 341,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(a)  #中位数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.401634Z",
     "end_time": "2023-04-03T16:23:45.061586Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "outputs": [
    {
     "data": {
      "text/plain": "675"
     },
     "execution_count": 342,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()  #求和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.404085Z",
     "end_time": "2023-04-03T16:23:45.061692Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "outputs": [
    {
     "data": {
      "text/plain": "35.625833323586974"
     },
     "execution_count": 343,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std()  #标准差"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.408427Z",
     "end_time": "2023-04-03T16:23:45.061801Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "outputs": [
    {
     "data": {
      "text/plain": "1269.2"
     },
     "execution_count": 344,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var()  #方差"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.410842Z",
     "end_time": "2023-04-03T16:23:45.061911Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 94,  98, 114, 204, 207, 257, 297, 308, 406, 454, 511, 593, 596,\n       674, 675])"
     },
     "execution_count": 345,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum()  #累加和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.413503Z",
     "end_time": "2023-04-03T16:23:45.062021Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "outputs": [
    {
     "data": {
      "text/plain": "array([                 94,                 376,                6016,\n                    541440,             1624320,            81216000,\n                3248640000,         35735040000,       3502033920000,\n           168097628160000,    9581564805120000,  785688314019840000,\n       2357064942059520000, -616375256452956160, -616375256452956160])"
     },
     "execution_count": 346,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumprod()  #累乘和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.417457Z",
     "end_time": "2023-04-03T16:23:45.062138Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "outputs": [
    {
     "data": {
      "text/plain": "14"
     },
     "execution_count": 347,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin()  #最小值的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.419738Z",
     "end_time": "2023-04-03T16:23:45.062250Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "outputs": [
    {
     "data": {
      "text/plain": "8"
     },
     "execution_count": 348,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax()  #最大值的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.422552Z",
     "end_time": "2023-04-03T16:23:45.062357Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[4, 1, 2, 3, 0],\n       [2, 1, 4, 0, 3],\n       [4, 2, 0, 3, 1]])"
     },
     "execution_count": 349,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argsort()  #返回排序的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.425416Z",
     "end_time": "2023-04-03T16:23:45.062466Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 350,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0],\n       [0, 3],\n       [1, 3],\n       [2, 0],\n       [2, 1],\n       [2, 3]])"
     },
     "execution_count": 350,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere(a > 50)  #符合条件的索引\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.427467Z",
     "end_time": "2023-04-03T16:23:45.062569Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 351,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "94\n",
      "90\n",
      "98\n",
      "57\n",
      "82\n",
      "78\n"
     ]
    }
   ],
   "source": [
    "for (i, j) in index:\n",
    "    print(a[i, j])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.429264Z",
     "end_time": "2023-04-03T16:23:45.062679Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 352,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[2161.8 ,  943.55,  901.9 ],\n       [ 943.55,  981.8 ,  734.4 ],\n       [ 901.9 ,  734.4 , 1574.7 ]])"
     },
     "execution_count": 352,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#协方差，方差是数据内部做计算，协方差是两个数据之间做计算\n",
    "np.cov(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.432776Z",
     "end_time": "2023-04-03T16:23:45.062795Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 353,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1.        , 0.64765787, 0.4888231 ],\n       [0.64765787, 1.        , 0.59063916],\n       [0.4888231 , 0.59063916, 1.        ]])"
     },
     "execution_count": 353,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#相关性系数，[1, -1] 1正相关，-1负相关，0没关系\n",
    "np.corrcoef(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.435351Z",
     "end_time": "2023-04-03T16:23:45.062900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 线性代数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 矩阵运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[7, 9, 4],\n       [5, 1, 3],\n       [2, 4, 8]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[7, 0, 0, 3],\n       [5, 1, 4, 0],\n       [7, 0, 6, 2]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = np.random.randint(0, 10, size=(3, 3))\n",
    "B = np.random.randint(0, 10, size=(3, 4))\n",
    "display(A, B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.445043Z",
     "end_time": "2023-04-03T16:23:45.151965Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 355,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[122,   9,  60,  29],\n       [ 61,   1,  22,  21],\n       [ 90,   4,  64,  22]])"
     },
     "execution_count": 355,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B)  #矩阵乘法，点乘\n",
    "# np.dot(A, B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.449380Z",
     "end_time": "2023-04-03T16:23:45.152194Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 356,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[122,   9,  60,  29],\n       [ 61,   1,  22,  21],\n       [ 90,   4,  64,  22]])"
     },
     "execution_count": 356,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B  #表示矩阵运算"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.451069Z",
     "end_time": "2023-04-03T16:23:45.152317Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 357,
   "outputs": [],
   "source": [
    "C = np.random.randint(0, 10, size=(4, 5))\n",
    "# A.dot(C)  #形状不匹配"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.453947Z",
     "end_time": "2023-04-03T16:23:45.152373Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 鸢尾花实战"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 358,
   "outputs": [
    {
     "data": {
      "text/plain": "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
     },
     "execution_count": 358,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.loadtxt('./iris.csv')\n",
    "iris  #%% md\n",
    "## 基本操作"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.471960Z",
     "end_time": "2023-04-03T16:23:45.162471Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组创建"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 359,
   "outputs": [],
   "source": [
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.503743Z",
     "end_time": "2023-04-03T16:23:45.364746Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 360,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, 4])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l = [1, 2, 3, 4]\n",
    "# 将list转换为numpy数组\n",
    "nd1 = np.array(l)\n",
    "display(nd1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.505302Z",
     "end_time": "2023-04-03T16:23:45.365026Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 361,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0, 0],\n       [0, 0, 0, 0],\n       [0, 0, 0, 0]], dtype=int16)"
     },
     "execution_count": 361,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape=(3, 4), dtype=np.int16)\n",
    "nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.509352Z",
     "end_time": "2023-04-03T16:23:45.365146Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 362,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.],\n       [1., 1., 1., 1., 1.]], dtype=float32)"
     },
     "execution_count": 362,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape=(3, 5), dtype=np.float32)\n",
    "nd3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.513708Z",
     "end_time": "2023-04-03T16:23:45.365250Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n\n       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n\n       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])"
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 = np.full(shape=(3, 4, 5), fill_value=3.1415926)\n",
    "nd4"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.515479Z",
     "end_time": "2023-04-03T16:23:45.365372Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 364,
   "outputs": [
    {
     "data": {
      "text/plain": "array([27, 78,  2, 50, 72, 71, 54, 40,  2, 60, 67, 27, 52, 62,  8, 43, 69,\n       69, 90, 11])"
     },
     "execution_count": 364,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0, 100, size=20)\n",
    "nd5"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.518595Z",
     "end_time": "2023-04-03T16:23:45.365475Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 365,
   "outputs": [
    {
     "data": {
      "text/plain": "0.62365129790312"
     },
     "execution_count": 365,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 = np.random.rand()  # 0,1 之间的随机数\n",
    "nd6"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.522058Z",
     "end_time": "2023-04-03T16:23:45.365582Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 366,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-0.22008397,  2.00077283,  1.34249474,  1.13915152,  0.1271266 ],\n       [ 0.16924835, -0.04057074, -0.13935828,  0.17970181,  1.17351356],\n       [-0.62184916, -1.31551516,  0.41380233, -0.68816014, -0.40431251]])"
     },
     "execution_count": 366,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd7 = np.random.randn(3, 5)  # 正态分布，平均是0，标准差是1\n",
    "nd7"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.525052Z",
     "end_time": "2023-04-03T16:23:45.365687Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 367,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[179.68691205, 171.69097458, 174.65815476, 162.68803389,\n        163.15403022],\n       [181.66183417, 174.5267905 , 185.06061079, 171.75706219,\n        168.45471435],\n       [163.08043301, 182.74409087, 172.70649896, 183.85793419,\n        164.33941075]])"
     },
     "execution_count": 367,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd8 = np.random.normal(loc=175, scale=10, size=(3, 5))  # 正态分布，平均是loc，标准差是scale\n",
    "nd8"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.527776Z",
     "end_time": "2023-04-03T16:23:45.366521Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 368,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n       35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,\n       69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99])"
     },
     "execution_count": 368,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd9 = np.arange(1, 100, step=2)  #等差数列 左闭右开\n",
    "nd9"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.532001Z",
     "end_time": "2023-04-03T16:23:45.467716Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "outputs": [
    {
     "data": {
      "text/plain": "array([  0.        ,   2.02020202,   4.04040404,   6.06060606,\n         8.08080808,  10.1010101 ,  12.12121212,  14.14141414,\n        16.16161616,  18.18181818,  20.2020202 ,  22.22222222,\n        24.24242424,  26.26262626,  28.28282828,  30.3030303 ,\n        32.32323232,  34.34343434,  36.36363636,  38.38383838,\n        40.4040404 ,  42.42424242,  44.44444444,  46.46464646,\n        48.48484848,  50.50505051,  52.52525253,  54.54545455,\n        56.56565657,  58.58585859,  60.60606061,  62.62626263,\n        64.64646465,  66.66666667,  68.68686869,  70.70707071,\n        72.72727273,  74.74747475,  76.76767677,  78.78787879,\n        80.80808081,  82.82828283,  84.84848485,  86.86868687,\n        88.88888889,  90.90909091,  92.92929293,  94.94949495,\n        96.96969697,  98.98989899, 101.01010101, 103.03030303,\n       105.05050505, 107.07070707, 109.09090909, 111.11111111,\n       113.13131313, 115.15151515, 117.17171717, 119.19191919,\n       121.21212121, 123.23232323, 125.25252525, 127.27272727,\n       129.29292929, 131.31313131, 133.33333333, 135.35353535,\n       137.37373737, 139.39393939, 141.41414141, 143.43434343,\n       145.45454545, 147.47474747, 149.49494949, 151.51515152,\n       153.53535354, 155.55555556, 157.57575758, 159.5959596 ,\n       161.61616162, 163.63636364, 165.65656566, 167.67676768,\n       169.6969697 , 171.71717172, 173.73737374, 175.75757576,\n       177.77777778, 179.7979798 , 181.81818182, 183.83838384,\n       185.85858586, 187.87878788, 189.8989899 , 191.91919192,\n       193.93939394, 195.95959596, 197.97979798, 200.        ])"
     },
     "execution_count": 369,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd10 = np.linspace(0, 200, num=100)  #左闭右闭 生成num个\n",
    "nd10"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.535276Z",
     "end_time": "2023-04-03T16:23:45.467972Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 查看数组属性"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 370,
   "outputs": [],
   "source": [
    "nd = np.random.randn(5, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.538613Z",
     "end_time": "2023-04-03T16:23:45.468052Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "outputs": [
    {
     "data": {
      "text/plain": "(5, 3)"
     },
     "execution_count": 371,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.shape  #数组形状"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.540646Z",
     "end_time": "2023-04-03T16:23:45.468183Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 372,
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('float64')"
     },
     "execution_count": 372,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype  #数组的数据类型"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.543427Z",
     "end_time": "2023-04-03T16:23:45.468297Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 373,
   "outputs": [
    {
     "data": {
      "text/plain": "15"
     },
     "execution_count": 373,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.size  #数组的尺寸"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.545796Z",
     "end_time": "2023-04-03T16:23:45.468421Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 374,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 374,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.ndim  #数组维度"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.549119Z",
     "end_time": "2023-04-03T16:23:45.468528Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 375,
   "outputs": [
    {
     "data": {
      "text/plain": "8"
     },
     "execution_count": 375,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.itemsize  # 单挑数据长度"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.551340Z",
     "end_time": "2023-04-03T16:23:45.468643Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 文件读写"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 376,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[22, 40, 23, 47, 23],\n       [88, 42, 90, 29, 39],\n       [74, 71, 63, 10,  9]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[ 0.81269771,  0.3565066 ,  0.3834412 , -0.05829243,  0.1136959 ],\n       [ 0.33736193, -0.68674291,  0.56249361,  0.26744188, -1.08902846],\n       [-0.94684841,  0.80844149,  0.56885954,  1.47093152, -0.82400095]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 100, size=(3, 5))\n",
    "nd2 = np.random.randn(3, 5)\n",
    "display(nd1, nd2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.568242Z",
     "end_time": "2023-04-03T16:23:45.519306Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 377,
   "outputs": [],
   "source": [
    "np.save('./data', nd1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.571483Z",
     "end_time": "2023-04-03T16:23:45.519410Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 378,
   "outputs": [
    {
     "data": {
      "text/plain": "<function IPython.core.display_functions.display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, raw=False, clear=False, **kwargs)>"
     },
     "execution_count": 378,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.load('./data.npy')\n",
    "display"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.574071Z",
     "end_time": "2023-04-03T16:23:45.519529Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 数组运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 基本运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 379,
   "outputs": [
    {
     "data": {
      "text/plain": "array([9, 7, 0, 3, 3])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([0, 1, 7, 5, 1])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 10, size=5)\n",
    "nd2 = np.random.randint(0, 10, size=5)\n",
    "display(nd1, nd2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.632853Z",
     "end_time": "2023-04-03T16:23:45.627343Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 380,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 9,  6, -7, -2,  2])"
     },
     "execution_count": 380,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 - nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.636752Z",
     "end_time": "2023-04-03T16:23:45.627511Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 381,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  7,  0, 15,  3])"
     },
     "execution_count": 381,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.640020Z",
     "end_time": "2023-04-03T16:23:45.627621Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 382,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/g_/dxml3hms52dfwqrcbt4fdmtw0000gn/T/ipykernel_44677/2467826621.py:1: RuntimeWarning: divide by zero encountered in divide\n",
      "  nd1 / nd2\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([inf, 7. , 0. , 0.6, 3. ])"
     },
     "execution_count": 382,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 / nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.643349Z",
     "end_time": "2023-04-03T16:23:45.627782Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 383,
   "outputs": [
    {
     "data": {
      "text/plain": "array([  1,   7,   0, 243,   3])"
     },
     "execution_count": 383,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 ** nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.647720Z",
     "end_time": "2023-04-03T16:23:45.627901Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 384,
   "outputs": [
    {
     "data": {
      "text/plain": "4.605170185988092"
     },
     "execution_count": 384,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(100)  #自然底数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.650053Z",
     "end_time": "2023-04-03T16:23:45.628006Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 385,
   "outputs": [
    {
     "data": {
      "text/plain": "8"
     },
     "execution_count": 385,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(2, 3)  #2的3次幂"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.653306Z",
     "end_time": "2023-04-03T16:23:45.628108Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 逻辑运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 386,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ True,  True, False, False,  True])"
     },
     "execution_count": 386,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 > nd2  # 对应位置进行比较"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.663143Z",
     "end_time": "2023-04-03T16:23:45.647326Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组与标量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 387,
   "outputs": [
    {
     "data": {
      "text/plain": "array([19, 17, 10, 13, 13])"
     },
     "execution_count": 387,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 + 10  #所有位置都+10，这种操作叫广播"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.695812Z",
     "end_time": "2023-04-03T16:23:45.697889Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 388,
   "outputs": [
    {
     "data": {
      "text/plain": "array([18, 14,  0,  6,  6])"
     },
     "execution_count": 388,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.698948Z",
     "end_time": "2023-04-03T16:23:45.698035Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 389,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/g_/dxml3hms52dfwqrcbt4fdmtw0000gn/T/ipykernel_44677/3480916047.py:1: RuntimeWarning: divide by zero encountered in divide\n",
      "  1 / nd1\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([0.11111111, 0.14285714,        inf, 0.33333333, 0.33333333])"
     },
     "execution_count": 389,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / nd1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.701512Z",
     "end_time": "2023-04-03T16:23:45.698232Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### -=\\+=\\*= 直接改变数组"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 390,
   "outputs": [],
   "source": [
    "nd1 += 100"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.727149Z",
     "end_time": "2023-04-03T16:23:45.728945Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 391,
   "outputs": [
    {
     "data": {
      "text/plain": "array([109, 107, 100, 103, 103])"
     },
     "execution_count": 391,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.729977Z",
     "end_time": "2023-04-03T16:23:45.729242Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 复制和视图"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 赋值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 392,
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=5)\n",
    "b = a  #赋值操作，只是指针指向同一个对象"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.790228Z",
     "end_time": "2023-04-03T16:23:45.769763Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 393,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 393,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.793203Z",
     "end_time": "2023-04-03T16:23:45.770039Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 浅拷贝"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 394,
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=10)\n",
    "b = a.view()  #各元素是同一个对象，但数组本身是两个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.819554Z",
     "end_time": "2023-04-03T16:23:45.902815Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 395,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 395,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.822940Z",
     "end_time": "2023-04-03T16:23:45.903118Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 396,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 396,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.owndata"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.825392Z",
     "end_time": "2023-04-03T16:23:45.903229Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 397,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1024, 2048,    3,    7,    7,    6,    9,    6,    4,    7])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024, 2048,    3,    7,    7,    6,    9,    6,    4,    7])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048\n",
    "display(a, b)  #修改谁都是修改的元素内容本身，都会引起数组变化"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.828094Z",
     "end_time": "2023-04-03T16:23:45.903405Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 398,
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 10, size=10)\n",
    "b = a.copy()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.831224Z",
     "end_time": "2023-04-03T16:23:45.903461Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 399,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1024,    0,    7,    5,    0,    4,    4,    0,    2,    2])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([   9, 2048,    7,    5,    0,    4,    4,    0,    2,    2])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048\n",
    "display(a, b)  #修改的是数组自己的元素内容"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.834578Z",
     "end_time": "2023-04-03T16:23:45.903621Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 400,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n       9.9999998e+07, 9.9999999e+07])"
     },
     "execution_count": 400,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1e8)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.837330Z",
     "end_time": "2023-04-03T16:23:46.399274Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 401,
   "outputs": [],
   "source": [
    "b = a[[1, 3, 5, 7, 99]].copy()  #取出想要的数据后，删除a，防止内存占用\n",
    "del a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.988128Z",
     "end_time": "2023-04-03T16:23:46.399384Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 索引、切片、迭代"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 基本索引切片"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 402,
   "outputs": [
    {
     "data": {
      "text/plain": "array([24,  6,  2, 22, 26, 17,  4, 25,  6, 15])"
     },
     "execution_count": 402,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 30, size=10)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.991621Z",
     "end_time": "2023-04-03T16:23:46.399503Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 403,
   "outputs": [
    {
     "data": {
      "text/plain": "22"
     },
     "execution_count": 403,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.994426Z",
     "end_time": "2023-04-03T16:23:46.399610Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 404,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6, 22, 17])"
     },
     "execution_count": 404,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[[1, 3, 5]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.997257Z",
     "end_time": "2023-04-03T16:23:46.399718Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 405,
   "outputs": [
    {
     "data": {
      "text/plain": "array([24,  6,  2])"
     },
     "execution_count": 405,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:3]  #左闭右开"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:40.999740Z",
     "end_time": "2023-04-03T16:23:46.399853Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 406,
   "outputs": [
    {
     "data": {
      "text/plain": "array([24,  6,  2])"
     },
     "execution_count": 406,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:3]  #：前不写，默认0开始"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.002371Z",
     "end_time": "2023-04-03T16:23:46.399966Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 407,
   "outputs": [
    {
     "data": {
      "text/plain": "array([22, 26, 17,  4, 25,  6, 15])"
     },
     "execution_count": 407,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3:]  #：后不写，默认切到最后"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.004686Z",
     "end_time": "2023-04-03T16:23:46.400081Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 408,
   "outputs": [
    {
     "data": {
      "text/plain": "array([24,  2, 26,  4,  6])"
     },
     "execution_count": 408,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2]  # 每2个取1个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.007089Z",
     "end_time": "2023-04-03T16:23:46.400191Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 409,
   "outputs": [
    {
     "data": {
      "text/plain": "array([22,  4, 15])"
     },
     "execution_count": 409,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3::3]  #从3开始，每3个取1个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.009950Z",
     "end_time": "2023-04-03T16:23:46.400304Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 410,
   "outputs": [
    {
     "data": {
      "text/plain": "array([15,  6, 25,  4, 17, 26, 22,  2,  6, 24])"
     },
     "execution_count": 410,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]  #倒排"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.012393Z",
     "end_time": "2023-04-03T16:23:46.481749Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 6, 22, 17])"
     },
     "execution_count": 411,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:7:2]  #索引1开始，索引7结束，每2个取1个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.017495Z",
     "end_time": "2023-04-03T16:23:46.481908Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 412,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[17, 14, 12,  1,  4,  2,  0, 28, 12, 17],\n       [28,  4, 15, 13, 22,  0, 12,  4, 17,  8],\n       [ 9,  1,  7,  1, 22, 23, 17, 26,  8,  5],\n       [11,  8, 27,  2, 29, 26, 15, 14,  3, 19],\n       [18,  9, 23,  1,  2,  1, 22,  3, 21,  7],\n       [12, 15, 14,  0, 25, 18,  4, 28, 21, 16],\n       [11, 20, 15,  4,  7,  8, 15, 24, 21, 28],\n       [12,  3,  7,  6,  3, 29, 29, 24, 29, 28],\n       [ 9, 27, 21, 17,  4, 16, 22, 19, 25, 29],\n       [23, 19, 12, 14, 17, 19, 20,  7, 19,  9]])"
     },
     "execution_count": 412,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0, 30, size=(10, 10))\n",
    "b  #二维数组和一维一样"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.020411Z",
     "end_time": "2023-04-03T16:23:46.482026Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 413,
   "outputs": [
    {
     "data": {
      "text/plain": "array([28,  4, 15, 13, 22,  0, 12,  4, 17,  8])"
     },
     "execution_count": 413,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.023492Z",
     "end_time": "2023-04-03T16:23:46.482132Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 414,
   "outputs": [
    {
     "data": {
      "text/plain": "array([27, 26, 15])"
     },
     "execution_count": 414,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[3, [2, 5, 6]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.026237Z",
     "end_time": "2023-04-03T16:23:46.482236Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1, 22, 26],\n       [ 8, 29, 14],\n       [ 9,  2,  3],\n       [15, 25, 28],\n       [20,  7, 24]])"
     },
     "execution_count": 415,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2:7, 1::3]  #行：2-7，列：1开始，每3个取1个"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.029388Z",
     "end_time": "2023-04-03T16:23:46.482341Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 花式索引和索引技巧"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 416,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "execution_count": 416,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.032524Z",
     "end_time": "2023-04-03T16:23:46.482447Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 417,
   "outputs": [
    {
     "data": {
      "text/plain": "array([3, 4, 5, 6])"
     },
     "execution_count": 417,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[3:7]  #切片返回的数据不是深拷贝\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.035536Z",
     "end_time": "2023-04-03T16:23:46.482553Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 418,
   "outputs": [
    {
     "data": {
      "text/plain": "array([   0,    1,    2, 1024,    4,    5,    6,    7,    8,    9,   10,\n         11,   12,   13,   14,   15,   16,   17,   18,   19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024,    4,    5,    6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.038612Z",
     "end_time": "2023-04-03T16:23:46.482751Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([3, 4, 5, 6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "b = a[[3, 4, 5, 6]]  #花式索引：索引时给出索引的数组，是深拷贝方式\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.041891Z",
     "end_time": "2023-04-03T16:23:46.571409Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 420,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1024,    4,    5,    6])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.045543Z",
     "end_time": "2023-04-03T16:23:46.571690Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 421,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[131,  37,  71],\n       [132,  41, 109],\n       [ 52,  30, 138],\n       [149,  83, 112],\n       [ 45,  11, 131],\n       [ 22,  41,  45],\n       [ 52,  22,  19],\n       [ 97, 123, 109],\n       [144,  89,  20],\n       [ 19,  92, 123],\n       [ 15,  90, 102],\n       [  9,  11,   0],\n       [103,  65, 143],\n       [138,  61, 118],\n       [ 91, 143, 103],\n       [126, 118,  30],\n       [123,  25,  75],\n       [136,  17,  76],\n       [ 10,  62,  18],\n       [  5,  20,  66],\n       [  2, 100,  81],\n       [142, 108,  67],\n       [ 69,  80,  15],\n       [ 89, 134,  50],\n       [ 84, 121, 115],\n       [ 24,  26,   0],\n       [ 26,  51, 150],\n       [ 37,   9, 110],\n       [  9,  76,  36],\n       [ 33,  16,  26],\n       [102,  49,  40],\n       [ 81,   8,  37],\n       [ 23, 116,  31],\n       [110, 133,  17],\n       [ 38,  92, 133],\n       [104, 133, 138],\n       [ 45,  15, 132],\n       [ 16,  89,  59],\n       [126,  23,  46],\n       [116,  48,   6],\n       [112,   4,  97],\n       [ 78,  72, 135],\n       [150, 144,  64],\n       [129, 150,  91],\n       [ 63, 129, 126],\n       [  1, 133, 103],\n       [150,   4,  67],\n       [ 81,  28, 134],\n       [145,  52,  45],\n       [ 70,   6, 141],\n       [ 13,  58,  29],\n       [118,  92,  49],\n       [ 91,  53,   2],\n       [ 33, 147, 123],\n       [ 99, 110,  54],\n       [ 82,  78,  22],\n       [ 89, 114,  94],\n       [  6,  43,  69],\n       [ 72, 100,  39],\n       [ 96, 138,  37],\n       [ 70,  81,   4],\n       [ 37, 123, 130],\n       [117,  41,  54],\n       [ 16, 131,  17],\n       [ 85,  74,  30],\n       [ 28,  27, 123],\n       [ 80,  98,  41],\n       [ 33,  51,  87],\n       [ 26,  78,  72],\n       [ 48, 126, 130],\n       [142, 129, 134],\n       [ 31,  98,  50],\n       [ 20,  72,  89],\n       [ 72,  77,  53],\n       [109,  79,  61],\n       [ 80,  18,  25],\n       [  7,  48,  73],\n       [ 93,  54,  67],\n       [ 57, 119,  21],\n       [ 29,  55,  15],\n       [  6,  35, 107],\n       [146,  17, 139],\n       [  6,  61,  77],\n       [ 66,  67,  79],\n       [ 17, 126, 140],\n       [ 79,  54, 116],\n       [ 42,  43,  26],\n       [145, 131,  48],\n       [ 78, 116,  60],\n       [139, 107, 126],\n       [ 54, 120,  22],\n       [ 19,  82, 132],\n       [ 70,  98, 124],\n       [ 42, 135, 116],\n       [ 73, 136,  52],\n       [ 45, 142,  94],\n       [144,  83,  70],\n       [ 86, 147,  31],\n       [121,  84, 146],\n       [ 81,  45,  83]])"
     },
     "execution_count": 421,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 151, size=(100, 3))  #100名学生，3门考试的成绩\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.050029Z",
     "end_time": "2023-04-03T16:23:46.571853Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 422,
   "outputs": [
    {
     "data": {
      "text/plain": "array([131, 132, 138, 149, 131, 123, 144, 123, 143, 138, 143, 126, 123,\n       136, 142, 134, 121, 150, 133, 133, 133, 138, 132, 126, 135, 150,\n       144, 129, 150, 129, 126, 133, 150, 134, 145, 141, 147, 123, 138,\n       123, 130, 131, 123, 126, 130, 142, 129, 134, 146, 139, 126, 140,\n       145, 131, 139, 126, 120, 132, 124, 135, 136, 142, 144, 147, 121,\n       146])"
     },
     "execution_count": 422,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = a >= 120  #根据条件筛选数据\n",
    "a[cond]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.053101Z",
     "end_time": "2023-04-03T16:23:46.571961Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[142, 129, 134]])"
     },
     "execution_count": 423,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond2 = cond[:, 0] * cond[:, 1] * cond[:, 2]  #3个都>=120\n",
    "a[cond2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.056311Z",
     "end_time": "2023-04-03T16:23:46.572066Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 424,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 9, 11,  0],\n       [24, 26,  0]])"
     },
     "execution_count": 424,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = a >= 120\n",
    "cond2 = a <= 30\n",
    "a[cond2[:, 0] * cond2[:, 1] * cond2[:, 2]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.059050Z",
     "end_time": "2023-04-03T16:23:46.572171Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 形状操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组变形"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 425,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15, 18,  4, 19, 19],\n       [ 4, 17, 19,  1,  4],\n       [ 6, 10,  2, 18, 16]])"
     },
     "execution_count": 425,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 20, size=(3, 5))\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.062121Z",
     "end_time": "2023-04-03T16:23:46.572297Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 426,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15, 18,  4, 19, 19],\n       [ 4, 17, 19,  1,  4],\n       [ 6, 10,  2, 18, 16]])"
     },
     "execution_count": 426,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5, 3)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.065203Z",
     "end_time": "2023-04-03T16:23:46.572403Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 427,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[15]],\n\n       [[18]],\n\n       [[ 4]],\n\n       [[19]],\n\n       [[19]],\n\n       [[ 4]],\n\n       [[17]],\n\n       [[19]],\n\n       [[ 1]],\n\n       [[ 4]],\n\n       [[ 6]],\n\n       [[10]],\n\n       [[ 2]],\n\n       [[18]],\n\n       [[16]]])"
     },
     "execution_count": 427,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15, 1, 1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.068063Z",
     "end_time": "2023-04-03T16:23:46.572509Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 428,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15, 18,  4],\n       [19, 19,  4],\n       [17, 19,  1],\n       [ 4,  6, 10],\n       [ 2, 18, 16]])"
     },
     "execution_count": 428,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1, 3)  #-1表示数据，3自动计算-1=5"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.071294Z",
     "end_time": "2023-04-03T16:23:46.572658Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组转置"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 429,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15,  4,  6],\n       [18, 17, 10],\n       [ 4, 19,  2],\n       [19,  1, 18],\n       [19,  4, 16]])"
     },
     "execution_count": 429,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T  #行变列，列变行"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.074389Z",
     "end_time": "2023-04-03T16:23:46.673113Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 430,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[15,  4,  6],\n       [18, 17, 10],\n       [ 4, 19,  2],\n       [19,  1, 18],\n       [19,  4, 16]])"
     },
     "execution_count": 430,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a, (1, 0))  #与a.T相同，行0，列1，调整顺序(0,1)->(1,0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.078392Z",
     "end_time": "2023-04-03T16:23:46.673329Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 431,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[3, 0, 7, 6, 2, 3, 2],\n        [0, 9, 9, 0, 6, 5, 0],\n        [5, 2, 0, 0, 5, 4, 9],\n        [0, 1, 0, 2, 9, 2, 7],\n        [4, 1, 7, 7, 8, 3, 3]],\n\n       [[5, 9, 2, 2, 2, 5, 0],\n        [9, 4, 5, 7, 6, 4, 2],\n        [2, 4, 0, 9, 5, 3, 7],\n        [8, 4, 4, 9, 3, 7, 3],\n        [1, 0, 1, 1, 6, 8, 7]],\n\n       [[5, 2, 0, 6, 1, 1, 2],\n        [2, 5, 1, 3, 1, 3, 6],\n        [7, 7, 4, 0, 0, 7, 1],\n        [9, 8, 6, 1, 2, 3, 4],\n        [4, 4, 4, 4, 9, 1, 7]]])"
     },
     "execution_count": 431,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0, 10, size=(3, 5, 7))\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.080101Z",
     "end_time": "2023-04-03T16:23:46.673438Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 432,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[3, 5, 5],\n        [0, 9, 2],\n        [5, 2, 7],\n        [0, 8, 9],\n        [4, 1, 4]],\n\n       [[0, 9, 2],\n        [9, 4, 5],\n        [2, 4, 7],\n        [1, 4, 8],\n        [1, 0, 4]],\n\n       [[7, 2, 0],\n        [9, 5, 1],\n        [0, 0, 4],\n        [0, 4, 6],\n        [7, 1, 4]],\n\n       [[6, 2, 6],\n        [0, 7, 3],\n        [0, 9, 0],\n        [2, 9, 1],\n        [7, 1, 4]],\n\n       [[2, 2, 1],\n        [6, 6, 1],\n        [5, 5, 0],\n        [9, 3, 2],\n        [8, 6, 9]],\n\n       [[3, 5, 1],\n        [5, 4, 3],\n        [4, 3, 7],\n        [2, 7, 3],\n        [3, 8, 1]],\n\n       [[2, 0, 2],\n        [0, 2, 6],\n        [9, 7, 1],\n        [7, 3, 4],\n        [3, 7, 7]]])"
     },
     "execution_count": 432,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b, (2, 1, 0))  #(0,1,2)->(2,1,0)\n",
    "c"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.083894Z",
     "end_time": "2023-04-03T16:23:46.673546Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 433,
   "outputs": [
    {
     "data": {
      "text/plain": "(7, 5, 3)"
     },
     "execution_count": 433,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.086303Z",
     "end_time": "2023-04-03T16:23:46.673646Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数据堆叠合并"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 434,
   "outputs": [],
   "source": [
    "nd1 = np.random.randint(0, 10, size=(3, 5))\n",
    "nd2 = np.random.randint(0, 10, size=(3, 5))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.092149Z",
     "end_time": "2023-04-03T16:23:46.673696Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 435,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 5, 6, 4, 6],\n       [1, 3, 4, 9, 4],\n       [7, 2, 2, 0, 5],\n       [4, 8, 2, 7, 2],\n       [3, 3, 6, 2, 7],\n       [8, 2, 9, 0, 7]])"
     },
     "execution_count": 435,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1, nd2])  #默认合并后行增加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.094696Z",
     "end_time": "2023-04-03T16:23:46.673810Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 436,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 5, 6, 4, 6, 4, 8, 2, 7, 2],\n       [1, 3, 4, 9, 4, 3, 3, 6, 2, 7],\n       [7, 2, 2, 0, 5, 8, 2, 9, 0, 7]])"
     },
     "execution_count": 436,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1, nd2], axis=1)  #axis轴，第几个维度，行0，列1，列增加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.097230Z",
     "end_time": "2023-04-03T16:23:46.673913Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 437,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 5, 6, 4, 6, 4, 8, 2, 7, 2],\n       [1, 3, 4, 9, 4, 3, 3, 6, 2, 7],\n       [7, 2, 2, 0, 5, 8, 2, 9, 0, 7]])"
     },
     "execution_count": 437,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((nd1, nd2))  #堆叠，h水平，列增多"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.100259Z",
     "end_time": "2023-04-03T16:23:46.674015Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 438,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 5, 6, 4, 6],\n       [1, 3, 4, 9, 4],\n       [7, 2, 2, 0, 5],\n       [4, 8, 2, 7, 2],\n       [3, 3, 6, 2, 7],\n       [8, 2, 9, 0, 7]])"
     },
     "execution_count": 438,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((nd1, nd2))  #堆叠，v竖直，行增多"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.103079Z",
     "end_time": "2023-04-03T16:23:46.674116Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 数组拆分"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 439,
   "outputs": [],
   "source": [
    "a = np.random.randint(0, 100, size=(15, 10))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.105933Z",
     "end_time": "2023-04-03T16:23:46.674175Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 440,
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[41, 75, 58, 32, 98, 56, 13,  7, 16, 63],\n        [36, 48, 28, 70, 70, 11, 25, 29, 70, 32],\n        [24,  6, 22, 12,  6, 21, 13, 29, 73, 51],\n        [65, 56, 32, 23, 69, 69, 74, 16, 82, 93],\n        [73, 47, 63, 41, 72, 75, 54, 15, 30,  5]]),\n array([[66, 18, 19, 32, 40,  4, 39, 78, 91, 71],\n        [49, 45, 19, 44, 63, 42, 19, 69, 67, 84],\n        [88, 68, 78, 68, 77, 33, 58, 29, 37, 48],\n        [49, 67, 18, 46, 71, 25, 66,  7, 62, 25],\n        [ 2,  5, 69, 58, 66, 44, 57,  0,  4, 75]]),\n array([[ 3, 35, 88, 48, 51, 99, 54, 89, 59, 17],\n        [22,  1,  1, 28, 86, 64, 25, 85,  4, 77],\n        [51, 34, 84, 74, 20, 51, 32, 83, 64, 63],\n        [52, 38, 34, 27, 90, 16, 65, 36,  3, 75],\n        [77, 67, 35, 72, 53, 76, 97, 49, 15,  5]])]"
     },
     "execution_count": 440,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=3)  #平局分成多少份"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.109205Z",
     "end_time": "2023-04-03T16:23:46.821310Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 441,
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[41, 75, 58, 32, 98],\n        [36, 48, 28, 70, 70],\n        [24,  6, 22, 12,  6],\n        [65, 56, 32, 23, 69],\n        [73, 47, 63, 41, 72],\n        [66, 18, 19, 32, 40],\n        [49, 45, 19, 44, 63],\n        [88, 68, 78, 68, 77],\n        [49, 67, 18, 46, 71],\n        [ 2,  5, 69, 58, 66],\n        [ 3, 35, 88, 48, 51],\n        [22,  1,  1, 28, 86],\n        [51, 34, 84, 74, 20],\n        [52, 38, 34, 27, 90],\n        [77, 67, 35, 72, 53]]),\n array([[56, 13,  7, 16, 63],\n        [11, 25, 29, 70, 32],\n        [21, 13, 29, 73, 51],\n        [69, 74, 16, 82, 93],\n        [75, 54, 15, 30,  5],\n        [ 4, 39, 78, 91, 71],\n        [42, 19, 69, 67, 84],\n        [33, 58, 29, 37, 48],\n        [25, 66,  7, 62, 25],\n        [44, 57,  0,  4, 75],\n        [99, 54, 89, 59, 17],\n        [64, 25, 85,  4, 77],\n        [51, 32, 83, 64, 63],\n        [16, 65, 36,  3, 75],\n        [76, 97, 49, 15,  5]])]"
     },
     "execution_count": 441,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=2, axis=1)  #axis = 1 表示列被平均分成2分"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.110833Z",
     "end_time": "2023-04-03T16:23:46.821477Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 442,
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[41, 75, 58, 32, 98, 56, 13,  7, 16, 63]]),\n array([[36, 48, 28, 70, 70, 11, 25, 29, 70, 32],\n        [24,  6, 22, 12,  6, 21, 13, 29, 73, 51],\n        [65, 56, 32, 23, 69, 69, 74, 16, 82, 93],\n        [73, 47, 63, 41, 72, 75, 54, 15, 30,  5]]),\n array([[66, 18, 19, 32, 40,  4, 39, 78, 91, 71],\n        [49, 45, 19, 44, 63, 42, 19, 69, 67, 84],\n        [88, 68, 78, 68, 77, 33, 58, 29, 37, 48],\n        [49, 67, 18, 46, 71, 25, 66,  7, 62, 25]]),\n array([[ 2,  5, 69, 58, 66, 44, 57,  0,  4, 75],\n        [ 3, 35, 88, 48, 51, 99, 54, 89, 59, 17],\n        [22,  1,  1, 28, 86, 64, 25, 85,  4, 77],\n        [51, 34, 84, 74, 20, 51, 32, 83, 64, 63],\n        [52, 38, 34, 27, 90, 16, 65, 36,  3, 75],\n        [77, 67, 35, 72, 53, 76, 97, 49, 15,  5]])]"
     },
     "execution_count": 442,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=[1, 5, 9])  #0-1,1-5,5-9 根据索引切分"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.114495Z",
     "end_time": "2023-04-03T16:23:46.821592Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 443,
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[41, 75, 58, 32, 98],\n        [36, 48, 28, 70, 70],\n        [24,  6, 22, 12,  6],\n        [65, 56, 32, 23, 69],\n        [73, 47, 63, 41, 72],\n        [66, 18, 19, 32, 40],\n        [49, 45, 19, 44, 63],\n        [88, 68, 78, 68, 77],\n        [49, 67, 18, 46, 71],\n        [ 2,  5, 69, 58, 66],\n        [ 3, 35, 88, 48, 51],\n        [22,  1,  1, 28, 86],\n        [51, 34, 84, 74, 20],\n        [52, 38, 34, 27, 90],\n        [77, 67, 35, 72, 53]]),\n array([[56, 13,  7, 16, 63],\n        [11, 25, 29, 70, 32],\n        [21, 13, 29, 73, 51],\n        [69, 74, 16, 82, 93],\n        [75, 54, 15, 30,  5],\n        [ 4, 39, 78, 91, 71],\n        [42, 19, 69, 67, 84],\n        [33, 58, 29, 37, 48],\n        [25, 66,  7, 62, 25],\n        [44, 57,  0,  4, 75],\n        [99, 54, 89, 59, 17],\n        [64, 25, 85,  4, 77],\n        [51, 32, 83, 64, 63],\n        [16, 65, 36,  3, 75],\n        [76, 97, 49, 15,  5]])]"
     },
     "execution_count": 443,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a, indices_or_sections=2)  #h水平，列方向平均分成2份"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.116393Z",
     "end_time": "2023-04-03T16:23:46.821711Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 444,
   "outputs": [
    {
     "data": {
      "text/plain": "[array([[41, 75, 58, 32, 98, 56, 13,  7, 16, 63],\n        [36, 48, 28, 70, 70, 11, 25, 29, 70, 32],\n        [24,  6, 22, 12,  6, 21, 13, 29, 73, 51]]),\n array([[65, 56, 32, 23, 69, 69, 74, 16, 82, 93],\n        [73, 47, 63, 41, 72, 75, 54, 15, 30,  5],\n        [66, 18, 19, 32, 40,  4, 39, 78, 91, 71],\n        [49, 45, 19, 44, 63, 42, 19, 69, 67, 84]]),\n array([[88, 68, 78, 68, 77, 33, 58, 29, 37, 48],\n        [49, 67, 18, 46, 71, 25, 66,  7, 62, 25],\n        [ 2,  5, 69, 58, 66, 44, 57,  0,  4, 75],\n        [ 3, 35, 88, 48, 51, 99, 54, 89, 59, 17]]),\n array([[22,  1,  1, 28, 86, 64, 25, 85,  4, 77],\n        [51, 34, 84, 74, 20, 51, 32, 83, 64, 63],\n        [52, 38, 34, 27, 90, 16, 65, 36,  3, 75],\n        [77, 67, 35, 72, 53, 76, 97, 49, 15,  5]])]"
     },
     "execution_count": 444,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a, indices_or_sections=[3, 7, 11])  #v竖直，行方向平均分成2份"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.119458Z",
     "end_time": "2023-04-03T16:23:46.821823Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 广播机制"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 445,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "execution_count": 445,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0, 1, 2, 3] * 3)\n",
    "arr1.sort()  #排序，从小到大\n",
    "arr1 = arr1.reshape(4, 3)\n",
    "arr1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.124372Z",
     "end_time": "2023-04-03T16:23:46.821937Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 446,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([1, 2, 3])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.array([1, 2, 3])\n",
    "display(arr1, arr2)  #形状不对应，但依然可以运算，numpy底层进行了广播"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.128485Z",
     "end_time": "2023-04-03T16:23:46.822112Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 2, 3],\n       [2, 3, 4],\n       [3, 4, 5],\n       [4, 5, 6]])"
     },
     "execution_count": 447,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行不够，广播行\n",
    "arr1 + arr2  #arr2和arr1的每一行进行相加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.131755Z",
     "end_time": "2023-04-03T16:23:46.822220Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 448,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0, 0],\n       [1, 1, 1],\n       [2, 2, 2],\n       [3, 3, 3]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[1],\n       [2],\n       [3],\n       [4]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([[1], [2], [3], [4]])\n",
    "display(arr1, arr3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.134425Z",
     "end_time": "2023-04-03T16:23:46.822384Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 449,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 1, 1],\n       [3, 3, 3],\n       [5, 5, 5],\n       [7, 7, 7]])"
     },
     "execution_count": 449,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列不够，广播列\n",
    "arr1 + arr3  #arr3和arr1的每一列进行相加"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.137981Z",
     "end_time": "2023-04-03T16:23:46.822495Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 450,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]],\n\n       [[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]],\n\n       [[0, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]]])"
     },
     "execution_count": 450,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0, 1, 2, 3, 4, 5, 6, 7] * 3).reshape(3, 4, 2)\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.141114Z",
     "end_time": "2023-04-03T16:23:46.822607Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 451,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 1],\n       [2, 3],\n       [4, 5],\n       [6, 7]])"
     },
     "execution_count": 451,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([0, 1, 2, 3, 4, 5, 6, 7]).reshape(4, 2)\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.144248Z",
     "end_time": "2023-04-03T16:23:46.822717Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 452,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]],\n\n       [[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]],\n\n       [[ 0,  2],\n        [ 4,  6],\n        [ 8, 10],\n        [12, 14]]])"
     },
     "execution_count": 452,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b  #b广播了3份后，与a做求和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.147238Z",
     "end_time": "2023-04-03T16:23:46.822829Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 通用函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 元素级和数字级方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 453,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1, -3, -5,  1,  5,  8,  9])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([ 8, -3,  4, -5, -4, -3, -2])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.array([-1, -3, -5, 1, 5, 8, 9])\n",
    "c = np.random.randint(-5, 10, size=7)\n",
    "display(a, c)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.174222Z",
     "end_time": "2023-04-03T16:23:47.253976Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 454,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 3, 5, 1, 5, 8, 9])"
     },
     "execution_count": 454,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(a)  #求绝对值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.176510Z",
     "end_time": "2023-04-03T16:23:47.254356Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 455,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  4,  9, 16, 36, 49])"
     },
     "execution_count": 455,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1, 4, 9, 16, 36, 49])\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.178502Z",
     "end_time": "2023-04-03T16:23:47.254473Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 456,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 2., 3., 4., 6., 7.])"
     },
     "execution_count": 456,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(b)  #开平方"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.181377Z",
     "end_time": "2023-04-03T16:23:47.254586Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 457,
   "outputs": [
    {
     "data": {
      "text/plain": "array([   1,   16,   81,  256, 1296, 2401])"
     },
     "execution_count": 457,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(b)  #平方"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.183914Z",
     "end_time": "2023-04-03T16:23:47.254708Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 458,
   "outputs": [
    {
     "data": {
      "text/plain": "7.38905609893065"
     },
     "execution_count": 458,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(2)  #自然底数e的n次幂"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.186195Z",
     "end_time": "2023-04-03T16:23:47.254818Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 459,
   "outputs": [
    {
     "data": {
      "text/plain": "2.995732273553991"
     },
     "execution_count": 459,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(20)  #自然底数对数求解"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.188867Z",
     "end_time": "2023-04-03T16:23:47.254918Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 460,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 460,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(np.pi / 2)  #正弦值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.191111Z",
     "end_time": "2023-04-03T16:23:47.255016Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 461,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 461,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(0)  #余弦值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.193363Z",
     "end_time": "2023-04-03T16:23:47.255141Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 462,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9999999999999999"
     },
     "execution_count": 462,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tan(np.pi / 4)  #正切值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.197465Z",
     "end_time": "2023-04-03T16:23:47.255244Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 463,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 8, -3,  4,  1,  5,  8,  9])"
     },
     "execution_count": 463,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.maximum(a, c)  #从a和c中选取最大的值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.199965Z",
     "end_time": "2023-04-03T16:23:47.329902Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 464,
   "outputs": [
    {
     "data": {
      "text/plain": "array([-1, -3, -5, -5, -4, -3, -2])"
     },
     "execution_count": 464,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.minimum(a, c)  #从a和c中选取最小的值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.203129Z",
     "end_time": "2023-04-03T16:23:47.330180Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 465,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 3, 0])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([-1, -3,  4,  8])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.array([1, 3, 0])\n",
    "nd2 = np.array([-1, -3, 4, 8])\n",
    "display(nd1, nd2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.205769Z",
     "end_time": "2023-04-03T16:23:47.330438Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 466,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 466,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.any()  #只要有True，0：false，非0：true"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.208848Z",
     "end_time": "2023-04-03T16:23:47.330552Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 467,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 467,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.all()  #所有都是True"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.211463Z",
     "end_time": "2023-04-03T16:23:47.330660Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 468,
   "outputs": [
    {
     "data": {
      "text/plain": "60"
     },
     "execution_count": 468,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([1, 2, 3, 4, 6])\n",
    "np.inner(a, b)  #内积 1*1+2*2+3*3+4*4+5*6"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.214464Z",
     "end_time": "2023-04-03T16:23:47.330765Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 469,
   "outputs": [
    {
     "data": {
      "text/plain": "array([26, 34, 26, 12, 34, 81, 99, 79, 63, 82, 80, 33, 97, 75, 74, 98, 64,\n       22, 23,  0, 50, 11,  7,  0, 39, 84, 82, 63, 34, 65])"
     },
     "execution_count": 469,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0, 100, size=30)\n",
    "nd1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.217345Z",
     "end_time": "2023-04-03T16:23:47.330891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 470,
   "outputs": [
    {
     "data": {
      "text/plain": "array([26, 34, 26, 12, 34, 80, 80, 79, 63, 80, 80, 33, 80, 75, 74, 80, 64,\n       22, 23, 10, 50, 11, 10, 10, 39, 80, 80, 63, 34, 65])"
     },
     "execution_count": 470,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.clip(nd1, 10, 80)  #小于10的变成10，大于80的变成80"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.220396Z",
     "end_time": "2023-04-03T16:23:47.330998Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 471,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0.38455486,  1.22517864, -0.35597637,  0.00480066,  1.25931792,\n       -1.04872095, -0.83773376, -1.3237283 ,  0.98214585, -0.32334806,\n        1.18062832, -0.42198778,  0.0067758 ,  0.27633087, -1.45596086,\n       -0.36712821,  1.54099246,  1.35237873, -1.62855669, -0.77932421])"
     },
     "execution_count": 471,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.random.randn(20)\n",
    "nd2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.222660Z",
     "end_time": "2023-04-03T16:23:47.331115Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 472,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0.38,  1.23, -0.36,  0.  ,  1.26, -1.05, -0.84, -1.32,  0.98,\n       -0.32,  1.18, -0.42,  0.01,  0.28, -1.46, -0.37,  1.54,  1.35,\n       -1.63, -0.78])"
     },
     "execution_count": 472,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.round(2)  #保留小数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.226006Z",
     "end_time": "2023-04-03T16:23:47.331264Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 473,
   "outputs": [
    {
     "data": {
      "text/plain": "3.0"
     },
     "execution_count": 473,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(2.7)  #向上取整"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.228298Z",
     "end_time": "2023-04-03T16:23:47.331383Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 474,
   "outputs": [
    {
     "data": {
      "text/plain": "array([3., 3., 3.])"
     },
     "execution_count": 474,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(np.array([2.7, 2.1, 2.05]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.231613Z",
     "end_time": "2023-04-03T16:23:47.407779Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 475,
   "outputs": [
    {
     "data": {
      "text/plain": "array([2., 2., 2.])"
     },
     "execution_count": 475,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(np.array([2.9, 2.1, 2.999]))  #向下取整"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.234706Z",
     "end_time": "2023-04-03T16:23:47.407930Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 476,
   "outputs": [
    {
     "data": {
      "text/plain": "16"
     },
     "execution_count": 476,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, size=(3, 3))\n",
    "np.trace(a)  #计算对角线上的和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.237389Z",
     "end_time": "2023-04-03T16:23:47.408064Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### where函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 477,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "nd1 = np.array([1, 3, 5, 7, 9])\n",
    "nd2 = np.array([2, 4, 6, 8, 10])\n",
    "cond = np.array([True, False, False, True, True])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.242476Z",
     "end_time": "2023-04-03T16:23:47.408139Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 478,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 4, 6, 7, 9])"
     },
     "execution_count": 478,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(cond, nd1, nd2)  #条件如果是True返回nd1的数据，如果是False返回nd2的数据"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.244784Z",
     "end_time": "2023-04-03T16:23:47.408266Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 479,
   "outputs": [
    {
     "data": {
      "text/plain": "array([48, 70,  3, 61, 44, 14, 20, 22, 18, 98, 74, 44, 56, 20, 87, 33, 44,\n       47, 92, 53, 18, 36, 39, 51, 58, 67, 26, 20, 91, 51, 10,  4, 90, 30,\n       69,  1, 52, 95, 84, 52,  6, 15, 82, 91, 32, 47, 30, 30, 85, 41])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([-100,   70, -100,   61, -100, -100, -100, -100, -100,   98,   74,\n       -100,   56, -100,   87, -100, -100, -100,   92,   53, -100, -100,\n       -100,   51,   58,   67, -100, -100,   91,   51, -100, -100,   90,\n       -100,   69, -100,   52,   95,   84,   52, -100, -100,   82,   91,\n       -100, -100, -100, -100,   85, -100])"
     },
     "execution_count": 479,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=50)\n",
    "display(a)\n",
    "np.where(a > 50, a, -100)  #大于50从a中取出，否则返回-100"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.247593Z",
     "end_time": "2023-04-03T16:23:47.408445Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 480,
   "outputs": [
    {
     "data": {
      "text/plain": "array([98, 53, 68, 35, 32, 40, 56, 66, 63, 95, 43, 56, 97, 43, 36, 84, 62,\n       44,  6, 26, 69, 32, 38, 70, 63,  8, 41, 42, 94,  3, 11, 62, 91, 89,\n       23, 44,  1, 38, 76, 45, 48, 67, 65, 19, 92, 25, 52, 79, 29, 35])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([98, 63, 68, 35, 32, 40, 66, 66, 63, 95, 43, 66, 97, 43, 36, 84, 62,\n       44,  6, 26, 69, 32, 38, 70, 63,  8, 41, 42, 94,  3, 11, 62, 91, 89,\n       23, 44,  1, 38, 76, 45, 48, 67, 65, 19, 92, 25, 62, 79, 29, 35])"
     },
     "execution_count": 480,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分数在50到59的自动加10\n",
    "a = np.random.randint(0, 100, size=50)\n",
    "display(a)\n",
    "cond = (a >= 50) & (a < 60)\n",
    "np.where(cond, a + 10, a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.250659Z",
     "end_time": "2023-04-03T16:23:47.408603Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 481,
   "outputs": [
    {
     "data": {
      "text/plain": "array([81, 75, 62, 52, 90, 89, 17, 90, 58, 21, 70, 17,  8, 45, 85, 26, 94,\n       43, 11,  4])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([ 4,  8, 11, 17, 17, 21, 26, 43, 45, 52, 58, 62, 70, 75, 81, 85, 89,\n       90, 90, 94])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=20)\n",
    "b = np.sort(a)  #原数组不改变\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.254019Z",
     "end_time": "2023-04-03T16:23:47.408758Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 482,
   "outputs": [],
   "source": [
    "a.sort()  #在原数组做的排序"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.256836Z",
     "end_time": "2023-04-03T16:23:47.408808Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 483,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n       17, 18, 19])"
     },
     "execution_count": 483,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = a.argsort()  #返回排序的索引\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.259120Z",
     "end_time": "2023-04-03T16:23:47.408916Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 484,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 4,  8, 11, 17, 17, 21, 26, 43, 45, 52, 58, 62, 70, 75, 81, 85, 89,\n       90, 90, 94])"
     },
     "execution_count": 484,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#根据索引取出数据\n",
    "a[index]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.261694Z",
     "end_time": "2023-04-03T16:23:47.409039Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 集合操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 485,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 2, 19,  1, 16, 12,  1, 13, 17, 19,  4, 14, 18,  5,  7, 10])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([ 5, 27,  4, 11,  7,  8, 19,  0, 19, 27, 19, 21, 28, 13,  2])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 30, size=15)\n",
    "b = np.random.randint(0, 30, size=15)\n",
    "display(a, b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.265790Z",
     "end_time": "2023-04-03T16:23:47.506088Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 486,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 2,  4,  5,  7, 13, 19])"
     },
     "execution_count": 486,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(a, b)  #交集"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.269155Z",
     "end_time": "2023-04-03T16:23:47.506275Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 487,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  1,  2,  4,  5,  7,  8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 21,\n       27, 28])"
     },
     "execution_count": 487,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.union1d(a, b)  #并集"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.270707Z",
     "end_time": "2023-04-03T16:23:47.506398Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 488,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1, 10, 12, 14, 16, 17, 18])"
     },
     "execution_count": 488,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a, b)  #差集，a中有，b中没有"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.273863Z",
     "end_time": "2023-04-03T16:23:47.506508Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 统计函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 489,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[99, 82, 15, 32, 95],\n       [84, 29,  4, 14, 96],\n       [85,  5, 80, 19, 91]])"
     },
     "execution_count": 489,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0, 100, size=(3, 5))\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.305919Z",
     "end_time": "2023-04-03T16:23:47.793162Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 490,
   "outputs": [
    {
     "data": {
      "text/plain": "4"
     },
     "execution_count": 490,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.307585Z",
     "end_time": "2023-04-03T16:23:47.793335Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 491,
   "outputs": [
    {
     "data": {
      "text/plain": "array([99, 82, 80, 32, 96])"
     },
     "execution_count": 491,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis=0)  #方向，axis=0行，axis=1列"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.310513Z",
     "end_time": "2023-04-03T16:23:47.793462Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 492,
   "outputs": [
    {
     "data": {
      "text/plain": "55.333333333333336"
     },
     "execution_count": 492,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean()  #平均值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.314177Z",
     "end_time": "2023-04-03T16:23:47.793571Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 493,
   "outputs": [
    {
     "data": {
      "text/plain": "80.0"
     },
     "execution_count": 493,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(a)  #中位数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.317133Z",
     "end_time": "2023-04-03T16:23:47.793683Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 494,
   "outputs": [
    {
     "data": {
      "text/plain": "830"
     },
     "execution_count": 494,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()  #求和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.320059Z",
     "end_time": "2023-04-03T16:23:47.793828Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 495,
   "outputs": [
    {
     "data": {
      "text/plain": "36.95883596050552"
     },
     "execution_count": 495,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std()  #标准差"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.322689Z",
     "end_time": "2023-04-03T16:23:47.793940Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 496,
   "outputs": [
    {
     "data": {
      "text/plain": "1365.9555555555557"
     },
     "execution_count": 496,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var()  #方差"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.325254Z",
     "end_time": "2023-04-03T16:23:47.794048Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 497,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 99, 181, 196, 228, 323, 407, 436, 440, 454, 550, 635, 640, 720,\n       739, 830])"
     },
     "execution_count": 497,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum()  #累加和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.327879Z",
     "end_time": "2023-04-03T16:23:47.794167Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 498,
   "outputs": [
    {
     "data": {
      "text/plain": "array([                  99,                 8118,               121770,\n                    3896640,            370180800,          31095187200,\n               901760428800,        3607041715200,       50498584012800,\n           4847864065228800,   412068445544448000,  2060342227722240000,\n       -1193318445606764544, -4226306392818974720,  2787743801373884416])"
     },
     "execution_count": 498,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumprod()  #累乘和"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.330588Z",
     "end_time": "2023-04-03T16:23:47.794278Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 499,
   "outputs": [
    {
     "data": {
      "text/plain": "7"
     },
     "execution_count": 499,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin()  #最小值的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.333423Z",
     "end_time": "2023-04-03T16:23:47.794387Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 500,
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 500,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax()  #最大值的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.336053Z",
     "end_time": "2023-04-03T16:23:47.794493Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 501,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[2, 3, 1, 4, 0],\n       [2, 3, 1, 0, 4],\n       [1, 3, 2, 0, 4]])"
     },
     "execution_count": 501,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argsort()  #返回排序的索引"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.338587Z",
     "end_time": "2023-04-03T16:23:47.794599Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 502,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0],\n       [0, 1],\n       [0, 4],\n       [1, 0],\n       [1, 4],\n       [2, 0],\n       [2, 2],\n       [2, 4]])"
     },
     "execution_count": 502,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere(a > 50)  #符合条件的索引\n",
    "index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.342793Z",
     "end_time": "2023-04-03T16:23:47.794706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 503,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99\n",
      "82\n",
      "95\n",
      "84\n",
      "96\n",
      "85\n",
      "80\n",
      "91\n"
     ]
    }
   ],
   "source": [
    "for (i, j) in index:\n",
    "    print(a[i, j])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.344857Z",
     "end_time": "2023-04-03T16:23:47.794836Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 504,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1483.3 , 1414.45,  297.5 ],\n       [1414.45, 1754.8 ,  973.75],\n       [ 297.5 ,  973.75, 1653.  ]])"
     },
     "execution_count": 504,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#协方差，方差是数据内部做计算，协方差是两个数据之间做计算\n",
    "np.cov(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.347840Z",
     "end_time": "2023-04-03T16:23:47.794952Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 505,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1.        , 0.87671672, 0.18999232],\n       [0.87671672, 1.        , 0.57173823],\n       [0.18999232, 0.57173823, 1.        ]])"
     },
     "execution_count": 505,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#相关性系数，[1, -1] 1正相关，-1负相关，0没关系\n",
    "np.corrcoef(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.350491Z",
     "end_time": "2023-04-03T16:23:47.795065Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 线性代数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 矩阵运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 506,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[3, 9, 7],\n       [2, 5, 4],\n       [2, 4, 4]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[1, 3, 6, 3],\n       [8, 4, 9, 7],\n       [3, 5, 8, 6]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = np.random.randint(0, 10, size=(3, 3))\n",
    "B = np.random.randint(0, 10, size=(3, 4))\n",
    "display(A, B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.368199Z",
     "end_time": "2023-04-03T16:23:47.863769Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 507,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 96,  80, 155, 114],\n       [ 54,  46,  89,  65],\n       [ 46,  42,  80,  58]])"
     },
     "execution_count": 507,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B)  #矩阵乘法，点乘\n",
    "# np.dot(A, B)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.371983Z",
     "end_time": "2023-04-03T16:23:47.863999Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 508,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 96,  80, 155, 114],\n       [ 54,  46,  89,  65],\n       [ 46,  42,  80,  58]])"
     },
     "execution_count": 508,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B  #表示矩阵运算"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.374619Z",
     "end_time": "2023-04-03T16:23:47.864111Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 509,
   "outputs": [],
   "source": [
    "C = np.random.randint(0, 10, size=(4, 5))\n",
    "# A.dot(C)  #形状不匹配"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.377354Z",
     "end_time": "2023-04-03T16:23:47.864166Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 鸢尾花实战"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 510,
   "outputs": [
    {
     "data": {
      "text/plain": "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
     },
     "execution_count": 510,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.loadtxt('./iris.csv')\n",
    "iris"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:23:41.427074Z",
     "end_time": "2023-04-03T16:23:47.913138Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 511,
   "outputs": [
    {
     "data": {
      "text/plain": "array([4.3, 4.4, 4.4, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.7, 4.7, 4.8, 4.8,\n       4.8, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 4.9, 4.9, 5. , 5. , 5. , 5. ,\n       5. , 5. , 5. , 5. , 5. , 5. , 5.1, 5.1, 5.1, 5.1, 5.1, 5.1, 5.1,\n       5.1, 5.1, 5.2, 5.2, 5.2, 5.2, 5.3, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4,\n       5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.6, 5.6, 5.6, 5.6, 5.6, 5.6,\n       5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.8, 5.8, 5.8, 5.8, 5.8,\n       5.8, 5.8, 5.9, 5.9, 5.9, 6. , 6. , 6. , 6. , 6. , 6. , 6.1, 6.1,\n       6.1, 6.1, 6.1, 6.1, 6.2, 6.2, 6.2, 6.2, 6.3, 6.3, 6.3, 6.3, 6.3,\n       6.3, 6.3, 6.3, 6.3, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.5, 6.5,\n       6.5, 6.5, 6.5, 6.6, 6.6, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7,\n       6.8, 6.8, 6.8, 6.9, 6.9, 6.9, 6.9, 7. , 7.1, 7.2, 7.2, 7.2, 7.3,\n       7.4, 7.6, 7.7, 7.7, 7.7, 7.7, 7.9])"
     },
     "execution_count": 511,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#排序\n",
    "iris = np.sort(iris)\n",
    "iris"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:24:18.577044Z",
     "end_time": "2023-04-03T16:24:18.616412Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 513,
   "outputs": [
    {
     "data": {
      "text/plain": "array([4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5,\n       5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8,\n       6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.6, 7.7, 7.9])"
     },
     "execution_count": 513,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去重\n",
    "iris = np.unique(iris)\n",
    "iris"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:24:52.230667Z",
     "end_time": "2023-04-03T16:24:52.276642Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 514,
   "outputs": [
    {
     "data": {
      "text/plain": "210.39999999999998"
     },
     "execution_count": 514,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求和\n",
    "np.sum(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:25:14.992081Z",
     "end_time": "2023-04-03T16:25:15.144091Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 515,
   "outputs": [
    {
     "data": {
      "text/plain": "6.011428571428571"
     },
     "execution_count": 515,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求平均值\n",
    "np.mean(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:25:23.170857Z",
     "end_time": "2023-04-03T16:25:23.285254Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 516,
   "outputs": [
    {
     "data": {
      "text/plain": "array([  4.3,   8.7,  13.2,  17.8,  22.5,  27.3,  32.2,  37.2,  42.3,\n        47.5,  52.8,  58.2,  63.7,  69.3,  75. ,  80.8,  86.7,  92.7,\n        98.8, 105. , 111.3, 117.7, 124.2, 130.8, 137.5, 144.3, 151.2,\n       158.2, 165.3, 172.5, 179.8, 187.2, 194.8, 202.5, 210.4])"
     },
     "execution_count": 516,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求累加和\n",
    "np.cumsum(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:25:52.875553Z",
     "end_time": "2023-04-03T16:25:52.991581Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 518,
   "outputs": [
    {
     "data": {
      "text/plain": "6.0"
     },
     "execution_count": 518,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#中位数\n",
    "np.median(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:26:11.045311Z",
     "end_time": "2023-04-03T16:26:11.096249Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 519,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0587265306122449"
     },
     "execution_count": 519,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方差\n",
    "np.var(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:26:22.265826Z",
     "end_time": "2023-04-03T16:26:22.418218Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 520,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0289443768310533"
     },
     "execution_count": 520,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#标准差\n",
    "np.std(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:26:39.168983Z",
     "end_time": "2023-04-03T16:26:39.285893Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 521,
   "outputs": [
    {
     "data": {
      "text/plain": "4.3"
     },
     "execution_count": 521,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris.min()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:26:58.845116Z",
     "end_time": "2023-04-03T16:26:58.991887Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 522,
   "outputs": [
    {
     "data": {
      "text/plain": "7.9"
     },
     "execution_count": 522,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris.max()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:27:02.668529Z",
     "end_time": "2023-04-03T16:27:02.783623Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 523,
   "outputs": [
    {
     "data": {
      "text/plain": "7.9"
     },
     "execution_count": 523,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(iris)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-03T16:27:24.717454Z",
     "end_time": "2023-04-03T16:27:24.839436Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
